Spring-整合MyBatis
阿新 • • 發佈:2020-09-07
整合MyBatis
目錄
1. 步驟
1. 匯入相關jar包
-
junit
-
mybatis
-
mysql資料庫
-
spring相關的
-
aop織入
-
mybatis-spring [new]
<?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"> <parent> <artifactId>Spring-Study</artifactId> <groupId>com.wang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-10-mybatis</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</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.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!--spring操作資料庫的話,還需要有個spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> </dependencies> </project>
2. 編寫配置檔案
3. 測試
2. Mybatis-Spring
- 編寫資料來源
- sqlSessionFactory
- sqlSessionTemplate
- 需要給介面加實現類
- 將自己寫的實現類注入到Spring中
- 測試使用
1. User實體類
package com.wang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; private String name; private String pwd; }
2. UserMapper介面
package com.wang.mapper;
import com.wang.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> queryUser();
}
3. 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.wang.mapper.UserMapper">
<select id="queryUser" resultType="User">
select * from user;
</select>
</mapper>
4. 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"
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.DriverManagerDataSource
-->
<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=false&useUnicodeEncode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--繫結Mybatis配置檔案-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/wang/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate: 就是我們使用的SQLSession!-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能用構造器注入sqlSessionFactory, 因為它沒有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
5. mybatis-config.xml
<?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>
<!--設定管理-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--別名管理-->
<typeAliases>
<package name="com.wang.pojo"/>
</typeAliases>
</configuration>
6. applicationContext.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">
<import resource="Spring-dao.xml"/>
<bean id="userMapper" class="com.wang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
7. UserMapperImpl實現類
package com.wang.mapper;
import com.wang.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
//原來(只用Mybatis); 我們的所有操作都使用sqlSession來執行
//現在(Spring整合Mybatis): 我們使用sqlSessionTemplate
private SqlSessionTemplate sqlSession;
//為了在Spring中注入, 這裡要寫一個Setter
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> queryUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.queryUser();
}
}
8. 測試
import com.wang.mapper.UserMapper;
import com.wang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyTest {
@Test
public void test01 () {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
List<User> userList = userMapper.queryUser();
for (User user : userList) {
System.out.println(user);
}
}
}
3. 使用SqlSessionDaoSupport
介面和實體類與上面一致
- 配置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">
<!--DataSource: 使用Spring的資料來源替換Mybatis的配置
我們這裡使用Spring提供的JDBC: org.springframework.jdbc.datasource.DriverManagerDataSource
-->
<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=false&useUnicodeEncode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--繫結Mybatis配置檔案-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/wang/mapper/*.xml"/>
</bean>
</beans>
-
寫UserMapperImpl的實現類
注意, 此處要繼承SQLSessionDaoSupport!
package com.wang.mapper;
import com.wang.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> queryUser() {
return getSqlSession().getMapper(UserMapper.class).queryUser();
}
}
- 在spring中註冊
<?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">
<import resource="Spring-dao.xml"/>
<!--UserMapperImpl繼承的父類中要求注入sqlSessionFactory屬性-->
<bean id="userMapper2" class="com.wang.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>