SpringBoot 教程(6) 整合持久层技术
jdbcTemplate、mybatis
6.1 整合jdbcTemplate
6.1 pom.xml
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-jdbc
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba
</groupId>
<artifactId>druid-spring-boot-starter
</artifactId>
<version>1.1.10
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<scope>runtime
</scope>
<version>5.1.47
</version>
</dependency>
6.2 application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/feiyu
6.3 service
@Service
public class UserService {
@Autowired
JdbcTemplate jdbcTemplate
;
public Integer
addUser(User u
) {
return jdbcTemplate
.update("insert into user (username,address) values (?,?)", u
.getUsername(), u
.getAddress());
}
public Integer
updateUserNameById(User user
) {
return jdbcTemplate
.update("update user set username=? where id=?", user
.getUsername(), user
.getId());
}
public Integer
deleteUserById(Integer id
) {
return jdbcTemplate
.update("delete from user where id=?", id
);
}
public List
<User> getAllUser() {
return jdbcTemplate
.query("select id,username,address from user", new RowMapper<User>() {
@Override
public User
mapRow(ResultSet resultSet
, int i
) throws SQLException
{
int id
= resultSet
.getInt("id");
String username
= resultSet
.getString("username");
String address
= resultSet
.getString("address");
User user
= new User();
user
.setId(id
);
user
.setUsername(username
);
user
.setAddress(address
);
return user
;
}
});
}
public List
<User> getAllUser2() {
return jdbcTemplate
.query("select id,username,address from user", new BeanPropertyRowMapper<>(User
.class));
}
}
6.2 jdbcTemplate 多数据源
6.2.1 配置多个数据源
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix
= "spring.datasource.one")
DataSource
dsOne(){
return DruidDataSourceBuilder
.create().build();
}
@Bean
@ConfigurationProperties(prefix
= "spring.datasource.two")
DataSource
dsTwo(){
return DruidDataSourceBuilder
.create().build();
}
}
6.2.2 配置多个jdbcTemplate
@Configuration
public class JdbcTemplateConfig {
@Bean
@Primary
JdbcTemplate
jdbcTemplateOne(@Qualifier("dsOne") DataSource ds
){
return new JdbcTemplate(ds
);
}
@Bean
JdbcTemplate
jdbcTemplateTwo(@Qualifier("dsTwo") DataSource ds
){
return new JdbcTemplate(ds
);
}
6.2.3 使用
@Qualifier("jdbcTemplateOne")
@Autowired
JdbcTemplate jdbcTemplateOne
;
@Resource(name
= "jdbcTemplateTwo")
JdbcTemplate jdbcTemplateTwo
;
@Test
void test6() {
List
<User> allUser
= jdbcTemplateOne
.query("select id,username,address from user", new BeanPropertyRowMapper<>(User
.class));
System
.out
.println(allUser
);
List
<User> allUser2
= jdbcTemplateTwo
.query("select id,username,address from user", new BeanPropertyRowMapper<>(User
.class));
System
.out
.println(allUser2
);
}
6.3 整合mybatis
6.3.1 pom.xml
<dependency>
<groupId>com.alibaba
</groupId>
<artifactId>druid-spring-boot-starter
</artifactId>
<version>1.1.10
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<scope>runtime
</scope>
<version>5.1.47
</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot
</groupId>
<artifactId>mybatis-spring-boot-starter
</artifactId>
<version>2.1.0
</version>
</dependency>
6.3.2 mapper
public interface UserMapper {
List
<User> getAllUser();
}
6.3.3 MapperScan
@SpringBootApplication
@MapperScan(basePackages
="com.feiyuxuy.mybatis.mapper")
public class OrmApplication {
public static void main(String
[] args
) {
SpringApplication
.run(OrmApplication
.class, args
);
}
}
6.3.4 application.properties
mybatis.mapper-locations=classpath:/mapper/*.xml
6.4mybatis多数据源
6.4.1 pom.xml
<dependency>
<groupId>com.alibaba
</groupId>
<artifactId>druid-spring-boot-starter
</artifactId>
<version>1.1.10
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<scope>runtime
</scope>
<version>5.1.47
</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot
</groupId>
<artifactId>mybatis-spring-boot-starter
</artifactId>
<version>2.1.0
</version>
</dependency>
6.4.2 application.properties
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.url=jdbc:mysql://127.0.0.1:3306/feiyu
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.url=jdbc:mysql://127.0.0.1:3306/feiyu2
6.4.3 配置类
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix
= "spring.datasource.one")
DataSource
dsM1(){
return DruidDataSourceBuilder
.create().build();
}
@Bean
@ConfigurationProperties(prefix
= "spring.datasource.two")
DataSource
dsM2(){
return DruidDataSourceBuilder
.create().build();
}
}
@Configuration
@MapperScan(basePackages
= "com.feiyuxuy.orm.mapper",sqlSessionFactoryRef
= "sqlSessionFactory1",sqlSessionTemplateRef
= "sqlSessionTemplate1")
public class MybatisConfigOne {
@Resource(name
= "dsM1")
DataSource dsM1
;
@Bean
SqlSessionFactory
sqlSessionFactory1() {
SqlSessionFactoryBean bean
= new SqlSessionFactoryBean();
try {
bean
.setDataSource(dsM1
);
bean
.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper1/*.xml"));
return bean
.getObject();
} catch (Exception e
) {
e
.printStackTrace();
}
return null
;
}
@Bean
SqlSessionTemplate
sqlSessionTemplate1(){
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
@Configuration
@MapperScan(basePackages = "com.feiyuxuy.orm.mapper2",sqlSessionFactoryRef = "sqlSessionFactory2",sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MybatisConfigTwo {
@Resource(name = "dsM2")
DataSource dsm2;
@Bean
SqlSessionFactory sqlSessionFactory2() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
try {
bean.setDataSource(dsm2);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper2/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean
SqlSessionTemplate sqlSessionTemplate2(){
return new SqlSessionTemplate(sqlSessionFactory2());
}
}
6.4.4 mapper.java和xml
在mapper下新建UserMapper.java
public interface UserMapper {
List
<User> getAllUser();
}
在mapper2下新建UserMapper2.java
public interface UserMapper2 {
List<User> getAllUser2();
}
在resources下新建mapper1目录,新建UserMapper.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.feiyuxuy.orm.mapper.UserMapper">
<select id="getAllUser" resultType="com.feiyuxuy.orm.bean.User">
SELECT * FROM user
</select>
</mapper>
在resources下新建mapper2目录,新建UserMapper2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.feiyuxuy.orm.mapper2.UserMapper2">
<select id="getAllUser2" resultType="com.feiyuxuy.orm.bean.User">
SELECT * FROM user
</select>
</mapper>
6.4.5测试
@Autowired
UserMapper userMapper;
@Autowired
UserMapper2 userMapper2;
@Test
void test7() {
List<User> allUser = userMapper.getAllUser();
System.out.println(allUser);
List<User> allUser2 = userMapper2.getAllUser2();
System.out.println(allUser2);
}
源码地址:https://gitee.com/xuyou1028/spring-boot-tutorial