经过这两周的学习,把Mybatis与Spring过了一遍,简单的分享下两种整合Mybatis-Spring方式。
第一种实现整合方式:
先看结构:
在xml中,导入相对应的插件,如下:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.17</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
先建pojo包下的User(实体类):
package com.jin.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
UserMapper(接口):
注意:(选择interface,而不是class)
package com.jin.mapper;
import com.jin.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> selectUser();
}
UserMapper.xml:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:意思是“命名空间”,后面跟接口的路径。-->
<mapper namespace="com.jin.mapper.UserMapper">
<select id="selectUser" resultType="com.jin.pojo.User">
select * from mybatis01.users
</select>
</mapper>
UserMapperImpl:
package com.jin.mapper;
import com.jin.pojo.User;
import lombok.Data;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
@Data
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
再建resources资源文件包下的spring-dao.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--value中:写入自己的连接mysql信息-->
<!--使用Spring的数据源替换Mybatis的配置-->
<!--class中报红,看一下是不是没导入spring-jdbc插件,记得刷新Maven-->
<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/mybatis01?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--创建工厂bean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定Mybatis配置文件-->
<property name="mapperLocations" value="classpath:com/jin/mapper/*.xml"/>
</bean>
<!--使用SqlSessionFactory创建SqlSessionTemplate-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!--注入SqlSessionTemplate-->
<bean id="userMapper" class="com.jin.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
最后建个用来测试的 MyTest:
import com.jin.mapper.UserMapper;
import com.jin.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MyTest {
@Test
public void test() throws IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = (UserMapper) context.getBean("userMapper");
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
这样就可查询到mysql中的数据了,结果显示如下:
快速入口 : 整合Mybatis-Spring(二)
可以对比一下,这两种整合方式都有什么不同!
|