1.添加JDBC和连接池和MyBatis依赖
<!-- MySQL:Begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- MySQL:End -->
<!-- mybatis:Begin -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- mybatis:End -->
<!-- BoneCP连接池(JDBC):Begin -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<!-- BoneCP连接池(JDBC):End -->
2.写配置文件,配置到主程序中
配置文件:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
jdbc.period_in_minutes=60
jdbc.max_age_in_minutes=30
jdbc.max_connections_per_partition=100
jdbc.min_connections_per_partition=5
<?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>
</configuration>
主程序
package demo.boot;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.jolbox.bonecp.BoneCPDataSource;
@Configuration
@PropertySource(value = { "classpath:jdbc.properties" })
@SpringBootApplication
public class MainApplication {
@Value("${jdbc.driver}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.period_in_minutes}")
private int periodInMinutes;
@Value("${jdbc.max_age_in_minutes}")
private int maxAgeInMinutes;
@Value("${jdbc.max_connections_per_partition}")
private int maxConnectionsPerPartition;
@Value("${jdbc.min_connections_per_partition}")
private int minConnectionsPerPartition;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setDriverClass(driverClassName);
boneCPDataSource.setJdbcUrl(url);
boneCPDataSource.setUsername(username);
boneCPDataSource.setPassword(password);
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(periodInMinutes);
boneCPDataSource.setIdleMaxAgeInMinutes(maxAgeInMinutes);
boneCPDataSource.setMaxConnectionsPerPartition(maxConnectionsPerPartition);
boneCPDataSource.setMinConnectionsPerPartition(minConnectionsPerPartition);
return boneCPDataSource;
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
3.创建一个包mapper(dao)
4.创建接口
Mybatis是面向接口编程
package demo.boot.mapper;
import java.util.List;
import demo.boot.pojo.User;
public interface UserMapper {
public List<User> selectAll();
}
`
5.创建mappers包
在resources下创建mappers包,创建xml文件,注意名字一定要和mapper内的一致,要不然映射不到
<?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="demo.boot.mapper.UserMapper">
<select id="selectAll" resultType="User">
select * from t_user
</select>
</mapper>
6.创建Config包
创建的Mappers包下的文件SpringBoot程序并不知道,所以要创建一个config包来让SpringBoot程序识别mappers
6.1创建Cast类
package demo.boot.config;
public class Cast {
public static final String MYBATIS_CONFIG = "classpath:mybatis-config.xml";
public static final String PO_PACKAGE = "org.personal.qin.springboot.po";
public static final String MAPPER_PACKAGE = "org.personal.qin.springboot.mapper";
public static final String MAPPER_SCANNER = "classpath:mapper/*.xml";
}
6.2创建MapperScannerConfig类
package demo.boot.config;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage(Cast.MAPPER_PACKAGE);
return mapperScannerConfigurer;
}
}
6.3 创建MyBatisConfig类
package demo.boot.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
@Configuration
public class MyBatisConfig {
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resourceResolver.getResource(Cast.MYBATIS_CONFIG);
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
sqlSessionFactoryBean.setTypeAliasesPackage(Cast.PO_PACKAGE);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(Cast.MAPPER_SCANNER));
return sqlSessionFactoryBean.getObject();
}
}
这里只改Cast类中的内容
package demo.boot.config;
public class Cast {
public static final String MYBATIS_CONFIG = "classpath:mybatis-config.xml";
public static final String PO_PACKAGE = "demo.boot.pojo";
public static final String MAPPER_PACKAGE = "demo.boot.mapper";
public static final String MAPPER_SCANNER = "classpath:mappers/*.xml";
}
6.4 测试
|