<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.16version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
我们需要编写实体类
package cn.calendo.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String username;
private String password;
private String gender;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", address='" + address + '\'' +
'}';
}
}
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="cn.calendo.domain"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="cn.calendo.dao"/>
mappers>
configuration>
public class App {
public static void main(String[] args) throws IOException {
// 1. 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2. 加载SqlMapConfig.xml配置文件(getResourceAsStream会抛出异常)
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 3. 创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 4. 获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5. 执行SqlSession对象执行查询,获取结果User
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
Account ac = accountDao.findById(2);
System.out.println(ac);
sqlSession.close();
}
}
从此得出,我们应该让spring管理sqlsessionfactory
我们在config里面新建一个配置类,并添加@Compnent
与@ComponentScan("cn.calendo")
然后写一个jdbc的class(第三方bean管理格式)让我们的配置类去扫描它
package cn.calendo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
再写一个mybatis的配置类,并在springConfig里面注册一下即可
由于我们要创建sqlsessionfactory的代码量大,所以我们可以使用一个新的类来快速创建sqlsessionfactory
但是这样的话我们原来的xml和app主函数类就不能用了所以需要新建一个主函数类
public class App2 {
public static void main(String[] args) {
//获取核心配置类,然后核心配置类会去自己扫描其它配置类
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
//获取bean,我们选用service的,因为service里面可以去调用dao的
AccountService accountService = ctx.getBean(AccountService.class);
//写service的方法即可
Account accountServiceById = accountService.findById(2);
//输出
System.out.println(accountServiceById);
}
}
总体的流程是这样的:
1 pom.xml
导坐标
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.16version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
2 SpringConfig
的配置
@Configuration
@ComponentScan("cn.calendo")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
3 JdbcConfig
替代原xml来获取连接池信息
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
4 MybatisConfig
获取类型别名bean与映射bean
@Bean
// 引用类型注入的话在()内添加引用类型来传递值
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
// 获取工厂bean对象
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// 设置实体类区域
factoryBean.setTypeAliasesPackage("cn.calendo.domain");
// 这一段就是获取到引用类型内的数据
factoryBean.setDataSource(dataSource);
// 默认事务处理
return factoryBean;
}
//映射位置的bean
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("cn.calendo.dao");
return msc;
}
5 service层添加自动装配@Autowired
与注解注册@Service
6 主函数类里编写
public static void main(String[] args) {
//获取核心配置类,然后核心配置类会去自己扫描其它配置类
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
//获取bean,我们选用service的,因为service里面可以去调用dao的
AccountService accountService = ctx.getBean(AccountService.class);
//写service的方法即可
Account accountServiceById = accountService.findById(1);
//输出
System.out.println(accountServiceById);
}
我们先来测试业务层(更常见)下的包
先保证相关包已经导入pom
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.2.10.RELEASEversion>
dependency>