需要导入的包
<dependencies>
<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.9</version>
</dependency>
<!--Spring操作数据库的话,还需要Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>
<!--Aop织入-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class user {
private int id;
private String name;
private String pwd;
}
接口
public interface userMapper {
List<user> selectuser();
}
接口配置文件
<?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.Google.Mapper.userMapper">
<select id="selectuser" resultType="user">
select * from mybatis.user
</select>
</mapper>
配置spring(这里采用最原始的方法)
<?xml version="1.0" encoding="UTF8"?>
<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">
<!--dateSource 使用spring数据源配置代替Mybatis的数据源
这里使用 Spring中的JDBC包下的DriverManagerDataSource
-->
<bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dateSource"/>
<!--绑定Mybatis配置文件.这里Spring配置文件和Mybatis配置文件就连起来了-->
<property name="configLocation" value="classpath:Mybatis.xml"/>
<!--注册映射器-->
<property name="mapperLocations" value="classpath:com/Google/Mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!–SqlSessionTemplate类中只有有参构造,使用不了set注入–>
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
编写实现类
public class userMapperimpl implements userMapper{
private SqlSessionTemplate sqlSession;
public userMapperimpl(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<user> selectuser() {
userMapper mapper = sqlSession.getMapper(userMapper.class);
return mapper.selectuser();
}
}
注册bean
<bean id="userMapper" class="com.Google.Mapper.userMapperimpl">
<constructor-arg index="0" ref="sqlSession"/>
</bean>
测试
public class MyTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-Dao.xml");
userMapper userMappers = context.getBean("userMapper2", userMapper.class);
List<user> users = userMappers.selectuser();
for (user user : users) {
System.out.println(user);
}
}
}
结果
user(id=2, name=aaa, pwd=bbb)
user(id=3, name=李四, pwd=1231233)
user(id=4, name=李三, pwd=123123)
user(id=5, name=夏鸥, pwd=23234234)
更精简整合Mybatis
mybatis-Spring提供了一个父类SqlSessionDaoSupport。我们直接继承这个类,再去spring中注入参数sqlSessionFactory。我们再调用方法getSqlSession()就可以得到sqlSession。这里我们可以不在spring中去注册
class="org.mybatis.spring.SqlSessionTemplate">
实现类
public class userMapperimpl2 extends SqlSessionDaoSupport implements userMapper{
@Override
public List<user> selectuser() {
return getSqlSession().getMapper(userMapper.class).selectuser();
}
}
Spring配置
<?xml version="1.0" encoding="UTF8"?>
<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">
<!--dateSource 使用spring数据源配置代替Mybatis的数据源
这里使用 Spring中的JDBC包下的DriverManagerDataSource
-->
<bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dateSource"/>
<!--绑定Mybatis配置文件.这里Spring配置文件和Mybatis配置文件就连起来了-->
<property name="configLocation" value="classpath:Mybatis.xml"/>
<!--注册映射器-->
<property name="mapperLocations" value="classpath:com/Google/Mapper/*.xml"/>
</bean>
<bean id="userMapper2" class="com.Google.Mapper.userMapperimpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
实现还是与前一种方式一样