演示网站,wordpress免费媒体库管理,石城县网站建设,wordpress 支持手机数据持久化有几个常见的方案#xff0c;有 Spring 自带的 JdbcTemplate 、有 MyBatis#xff0c;还有 JPA#xff0c;在这些方案中#xff0c;最简单的就是 Spring 自带的 JdbcTemplate 了#xff0c;这个东西虽然没有 MyBatis 那么方便#xff0c;但是比起最开始的 Jdbc…数据持久化有几个常见的方案有 Spring 自带的 JdbcTemplate 、有 MyBatis还有 JPA在这些方案中最简单的就是 Spring 自带的 JdbcTemplate 了这个东西虽然没有 MyBatis 那么方便但是比起最开始的 Jdbc 已经强了很多了它没有 MyBatis 功能那么强大当然也意味着它的使用比较简单事实上JdbcTemplate 算是最简单的数据持久化方案了
一、创建一个 SpringBoot 项目
选择基本的 Web 依赖再记得选上 Jdbc 依赖以及数据库驱动依赖即可 项目创建成功之后记得添加 Druid 数据库连接池依赖注意这里可以添加专门为 Spring Boot 打造的 druid-spring-boot-starter而不是我们一般在 SSM 中添加的 Druid所有添加的依赖如下
dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.1.10/version
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.27/versionscoperuntime/scope
/dependency在 application.properties 中提供数据的基本配置
spring.datasource.typecom.alibaba.druid.pool.DruidDataSource
spring.datasource.usernameroot
spring.datasource.password123
spring.datasource.urljdbc:mysql:///test01?useUnicodetruecharacterEncodingUTF-8二、CRUD测试
①、创建Bean
public class User {private Long id;private String username;private String address;//省略getter/setter
}②、创建Service注入JdbcTemplate
Service
public class UserService {AutowiredJdbcTemplate jdbcTemplate;
}③、CRUD
JdbcTemplate 中除了查询有几个 API 之外增删改统一都使用 update 来操作自己来传入 SQL 即可。 update 方法的返回值就是 SQL 执行受影响的行数
//简单添加
public int addUser(User user){return jdbcTemplate.update(insert into user (username,address) values (?,?);, user.getUsername(), user.getAddress());
}/**复杂添加相当于完全使用了 JDBC 中的解决方案构建 PreparedStatement 时传入 Statement.RETURN_GENERATED_KEYS然后传入 KeyHolder最终从 KeyHolder 中获取刚刚插入数据的 id 保存到 user 对象的 id 属性中去
*/
public int addUser2(){KeyHolder keyHolder new GeneratedKeyHolder();int update jdbcTemplate.update(new PreparedStatementGreator(){Overridepublic PreparedStatement createPreparedStatement(Connection connection) throws SQLException {PreparedStatement ps connection.prepareStatement(insert into user (username,address) values (?,?);,Statement.RETURN_GENERATED_KEYS);ps.setString(1, user.getUsername());ps.setString(2, user.getAddress());return ps;}},keyHolder);user.setId(keyHolder.getKey().longValue());System.out.println(user);return update;
}//删除
public int deleteUserById(Long id){return jdbcTemplate.update(delete from user where id ?,id);
}//改
public int updateUserById(User user){return jdbcTemplate.update(update user set username?,address? where id?,user.getUsername(),user.getAddress(),user.getId());
}/**查询的时候需要提供一个 RowMapper就是需要自己手动映射将数据库中的字段和对象的属性一一对应起来
*/
public ListUser getAllUsers(){return jdbcTemplate.query(select * from user,new RowMapperUser(){Overridepublic User mapRow(ResultSet resultSet, int i) throws SQLException {String username resultSet.getString(username);String address resultSet.getString(address);long id resultSet.getLong(id);User user new User();user.setAddress(address);user.setUsername(username);user.setId(id);return user;}});
}//如果数据库中的字段和对象属性的名字一模一样的话有另外一个简单的方案
//查询时候传参也是使用占位符这个和前文的一致
public ListUser getAllUser2(){return jdbcTemplate.query(select * from user,new BeanPropertyRowMapper(User.class));
}多数据源
一、创建工程选择 Web、Jdbc 以及 MySQL 驱动
手动添加 Druid 依赖由于这里一会需要开发者自己配置 DataSoruce所以这里必须要使用 druid-spring-boot-starter 依赖而不是传统的那个 druid 依赖。 因为 druid-spring-boot-starter 依赖提供了 DruidDataSourceBuilder 类这个可以用来构建一个 DataSource 实例而传统的 Druid 则没有该类。
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.28/versionscoperuntime/scope
/dependency
dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.1.10/version
/dependency二、配置数据源
配置两个数据源
spring.datasource.one.urljdbc:mysql:///test01?useUnicodetruecharacterEncodingutf-8
spring.datasource.one.usernameroot
spring.datasource.one.passwordroot
spring.datasource.one.typecom.alibaba.druid.pool.DruidDataSourcespring.datasource.two.urljdbc:mysql:///test02?useUnicodetruecharacterEncodingutf-8
spring.datasource.two.usernameroot
spring.datasource.two.passwordroot
spring.datasource.two.typecom.alibaba.druid.pool.DruidDataSource加了 one 和 two 之后这里的配置就没法被 SpringBoot 自动加载了因为前面的 key 变了 需要我们自己去加载 DataSource 了此时需要自己配置一个 DataSourceConfig用来提供两个 DataSource Bean
Configuration
public class DataSourceConfig{BeanConfigurationProperties(prefixspring.datasource.one)//ConfigurationProperties 是 Spring Boot 提供的类型安全的属性绑定DataSource dsOne(){ //表示使用 spring.datasource.one 前缀的数据库配置去创建一个 DataSourcereturn DruidDataSourceBuilder.create().build();}BeanConfigurationProperties(prefixspring.datasource.two)DataSource dsTwo(){return DruidDataSourceBuilder.create().build();}
}三、配置JdbcTemplate实例
每一个 JdbcTemplate 的创建都需要一个 DataSource由于 Spring 容器中现在存在两个 DataSource默认使用类型查找会报错因此加上 Qualifier 注解表示按照名称查找
Configuration
public class JdbcTemplate{BeanJdbcTemplate jdbcTemplateOne(Qualifier(dsOne) DataSource dsOne){return new JdbcTemplate(dsOne);}BeanJdbcTemplate jdbcTemplateTwo(Qualifier(dsTwo) DataSource dsTwo){return new JdbcTemplate(dsTwo);}
}四、测试
RestController
public class HelloController{// Autowired 注解加上 Qualifier 注解两者联合起来实际上也是 byNameAutowiredQualifier(jdbcTemplateOne)JdbcTemplate jdbcTemplateOne;//使用 Resource 注解直接通过 byName 的方式注入进来Resource(namejdbcTemplateTwo)JdbcTemplate jdbcTemplateTwo;GetMapping(/user)public ListUser getAllUser(){ListUser list jdbcTemplateOne.query(select * from t_user, new BeanPropertyRowMapper(User.class));return list;}GetMapping(/user2)public ListUser getAllUser2() {ListUser list jdbcTemplateTwo.query(select * from t_user, new BeanPropertyRowMapper(User.class));return list;}
}