1.数据库
CREATE TABLE `NewTable` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`role_id` int(11) NULL DEFAULT NULL ,
`createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=13
ROW_FORMAT=DYNAMIC
2.添加Mybatis依赖
在已有的Spring项目中添加Mybatis依赖
pom.xml
<!--Spring-Mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
3.创建需要使用的类
User类
public class User {
private Integer id;
private String username;
private String password;
// set/get,构造函数及toString省略...
}
UserMapper映射类
public interface UserMapper {
/**
* 根据ID查User
* @param id
* @return
*/
User getUserById(@Param("id") Integer id);
}
IUserService
public interface IUserService {
/**
* 根据ID获取User记录
* @param id
* @return
*/
User getUserById(Integer id);
}
UserServiceImpl
@Service
public class UserServiceImpl implements IUserService {
@Autowired
UserMapper userMapper;
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
}
4.配置文件
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
validationQuery=1
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入属性-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--扫描包-->
<context:component-scan base-package="com.cx"/>
</beans>
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!--1.数据源-数据库连接池-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<!--
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
-->
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭Abandoned连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!--2.数据源注入到 SqlSession-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<!-- 为JAVA Bean起类别名 -->
<property name="typeAliasesPackage" value="com.cx.bean" />
<!--映射配置文件-->
<property name="mapperLocations" value="classpath:com/cx/mapper/*.xml" />
</bean>
<!--3.映射文件和接口的配置-->
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描com.cx.mapper这个包以及它的子包下的所有映射接口类 -->
<property name="basePackage" value="com.cx.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
5.测试功能
MybatisTest
public class MybatisTest {
@Autowired
IUserService userService;
@Before
public void setUp(){
// 从Spring容器取出UserService对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml","spring-mybatis.xml");
userService = applicationContext.getBean(IUserService.class);
}
@Test
public void test01() {
User user = userService.getUserById(2);
System.out.println(user);
}
}
结果