Spring整合Mybatis
需要maven包:
mysql-connector-java 5.1.47,
mybatis 3.5.2,
spring-webmvc 5.2.2.RELEASE,
spring-jdbc 5.2.2.RELEASE,
aspectjweaver 1.8.13
mybatis-spring 2.0.2,
目录:
pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>SpringMybatis</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency> </dependencies>
<!--maven由于他的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被处理或生效问题,解决方案-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
Mybatis-config.xml配置文件:
因为是spring可以完全掌管Mybatis但把mybatis的一些配置留给Mybatis-config比较好
<?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>
<typeAliases>
<package name="com.hdlf.pojo"/>
</typeAliases> <mappers>
<mapper resource="com/hdlf/mapper/UserMapper.xml"/>
</mappers>
</configuration>
UserMapper接口:
package com.hdlf.mapper; import com.hdlf.pojo.User; import java.util.List; public interface UserMapper {
public List<User> getalluser();
}
UserMapper.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hdlf.mapper.UserMapper">
<select id="getalluser" resultType="User">
select * from mybatis.user
</select>
</mapper>
User实体类:
package com.hdlf.pojo; public class User {
private int id;
private String name;
private String pwd; public User() {
} public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
重点,spring-config.xml配置:
根据官网上可以找到:
DataSource使用spring的数据源可以替换mybatis的配置,需要使用DriverManagerDataSource
sqlSessionFactory绑定mybatis配置文件,重点可以配合mybatis配置文件,而mybatis配置文件可以关联mappers
SqlSessionTemplate就是常用的sqlsession只是封装了,但需要构造器注入,把sqlSessionFactory绑定的配置注入到里面即可
<?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"> <!--DataSource:使用spring的数据源替换mybatis的配置
使用spring提供的jdbc:org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&UseUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定mybatis配置文件-->
<property name="configLocation" value="classpath:Mybatis-config.xml"/>
</bean> <!--SqlSessionTemplate:就是mybatis经常使用的SqlSession-->
<bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean> <bean id="usermapper" class="com.hdlf.mapper.UserMapperTemplate">
<property name="SqlSession" ref="SqlSession"></property> </bean>
</beans>
UserMapperTemplate实现类:
实现Usermapper的方法,封装SqlSessionTemplate,然后用getalluesr返回mapper.getalluser();所有数据
package com.hdlf.mapper; import com.hdlf.pojo.User;
import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperTemplate implements UserMapper { private SqlSessionTemplate SqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) {
this.SqlSession = sqlSession;
} public List<User> getalluser() {
UserMapper mapper = SqlSession.getMapper(UserMapper.class);
return mapper.getalluser();
} }
测试类:
不用配置mybatis之类的麻烦的配置
package com.hdlf.test; import com.hdlf.mapper.UserMapper;
import com.hdlf.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException; public class Mytest {
public static void main(String[] args) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserMapper userMapper = context.getBean("usermapper", UserMapper.class);
for (User user : userMapper.getalluser()) {
System.out.println(user); } }
}
结果: