项目准备
1.数据库表-tb_account.sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0 ;
DROP TABLE IF EXISTS ` tb_account` ;
CREATE TABLE ` tb_account` (
` id` int NOT NULL ,
` name` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
` money` double NULL DEFAULT NULL ,
PRIMARY KEY ( ` id` ) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO ` tb_account` VALUES ( 1 , 'tom' , 1000 ) ;
INSERT INTO ` tb_account` VALUES ( 2 , 'Jerry' , 500 ) ;
SET FOREIGN_KEY_CHECKS = 1 ;
2.数据层接口-AccountDao.java
package com. itheima. dao ;
import com. itheima. domain. Account ;
import org. apache. ibatis. annotations. Delete ;
import org. apache. ibatis. annotations. Insert ;
import org. apache. ibatis. annotations. Select ;
import org. apache. ibatis. annotations. Update ;
import java. util. List ;
public interface AccountDao {
@Insert ( "insert into tb_account( name, money) values(#{name},#{money})" )
void save ( Account account) ;
@Delete ( "delete from tb_account where id=#{id}" )
void delete ( Integer id) ;
@Update ( "update tb_account set name=#{name},money=#{money} where id=#{id}" )
void update ( Account account) ;
@Select ( "select * from tb_account" )
List < Account > findAll ( ) ;
@Select ( "select * from tb_account where id=#{id}" )
Account findById ( Integer id) ;
}
3.实体类对象Account.java
package com. itheima. domain ;
public class Account {
private Integer id;
private String name;
private Double money;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Double getMoney ( ) {
return money;
}
public void setMoney ( Double money) {
this . money = money;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}' ;
}
}
4.启动类App.java
package com. itheima ;
import com. itheima. dao. AccountDao ;
import com. itheima. domain. Account ;
import org. apache. ibatis. io. Resources ;
import org. apache. ibatis. session. SqlSession ;
import org. apache. ibatis. session. SqlSessionFactory ;
import org. apache. ibatis. session. SqlSessionFactoryBuilder ;
import java. io. IOException ;
import java. io. InputStream ;
public class App {
public static void main ( String [ ] args) throws IOException {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder ( ) ;
InputStream inputStream = Resources . getResourceAsStream ( "SqlMapConfig.xml" ) ;
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder. build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
AccountDao accountDao = sqlSession. getMapper ( AccountDao . class ) ;
Account ac = accountDao. findById ( 1 ) ;
System . out. println ( ac) ;
}
}
5.jdbc配置文件-jdbc.properties&SqlMapConfig.xml
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=12345678
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
< configuration>
< properties resource = " jdbc.properties" > </ properties>
< typeAliases>
< package name = " com.itheima.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 = " com.itheima.dao" />
</ mappers>
</ configuration>
6.导入坐标pom.xml
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.3.21</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.9</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.29</ version>
</ dependency>
</ dependencies>
Spring整合MyBatis
MyBatis程序核心对象分析
初始化SqlSessionFactory 获取连接,获取实现 获取数据层接口 关闭连接 整合MyBatis
初始化属性数据 初始化类型别名 初始化dataSource 初始化映射配置
1.导入坐标
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.2.11</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.3.19</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 2.0.6</ version>
</ dependency>
2.配置SpringConfig
@Configuration
@ComponentScan ( "com.itheima" )
@PropertySource ( "classpath:jdbc.properties" )
@Import ( { jdbcConfig. class , MyBatisConfig . class } )
public class SpringConfig {
}
3.Jdbc数据源配置
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 ds = new DruidDataSource ( ) ;
ds. setDriverClassName ( driver) ;
ds. setUrl ( url) ;
ds. setUsername ( userName) ;
ds. setPassword ( password) ;
return ds;
}
}
4.MyBatis配置
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory ( DataSource dataSource) {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean ( ) ;
ssfb. setTypeAliasesPackage ( "com.itheima.dmain" ) ;
ssfb. setDataSource ( dataSource) ;
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer ( ) {
MapperScannerConfigurer msc = new MapperScannerConfigurer ( ) ;
msc. setBasePackage ( "com.itheima.dao" ) ;
return msc;
}
}
5.相关准备
public interface AccountService {
void save ( Account account) ;
void update ( Account account) ;
void delete ( Integer id) ;
Account findById ( Integer id) ;
List < Account > findAll ( ) ;
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public void save ( Account account) {
accountDao. save ( account) ;
}
@Override
public void update ( Account account) {
accountDao. update ( account) ;
}
@Override
public void delete ( Integer id) {
accountDao. delete ( id) ;
}
@Override
public Account findById ( Integer id) {
return accountDao. findById ( id) ;
}
@Override
public List < Account > findAll ( ) {
return accountDao. findAll ( ) ;
}
}
public class App2 {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService accountService = ctx.getBean(AccountService.class);
Account ac = accountService.findById(1);
System.out.println(ac);
}
}
整合JUnit
1.导入坐标
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.13 .2 < / version>
< scope> test< / scope>
< / dependency>
< dependency>
< groupId> org. springframework< / groupId>
< artifactId> spring- test< / artifactId>
< version> 5.3 .19 < / version>
< / dependency>
2.使用Spring整合JUnit专用的类加载器
@RunWith ( SpringJUnit4ClassRunner . class )
@ContextConfiguration ( classes = SpringConfig . class )
public class AccountServiceTest {
@Autowired
private AccountService accountService;
@Test
public void testFindById ( ) {
System . out. println ( accountService. findById ( 1 ) ) ;
}
@Test
public void testFindAll ( ) {
System . out. println ( accountService. findAll ( ) ) ;