Java常用單詞
阿新 • • 發佈:2020-08-16
目錄
14、Spring整合Mybatis
官方參考文件:http://mybatis.org/spring/zh/index.html
問題:
(1)為什麼要整合Mybatis?
1、使用SqlSessionTemplate
步驟:
14.1.1、匯入依賴
所需依賴:
- Junit:測試所需依賴包
- Lombok:簡化實體類get、set等方法
- 引入Spring相關依賴
- 引入mybatis依賴
- 連線資料庫的驅動
- Spring整合mybatis所需依賴包
- 使用c3p0資料來源
<dependencies> <!-- 引入Junit測試類 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 引入LomBok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <!-- 引入Spring相關依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- 引入Mybatis依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- 引入連線資料庫的驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!-- Spring整合Mybatis的依賴包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- 使用c3p0資料來源 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.1</version> </dependency> <!-- Spring對jdbc的封裝 --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.5.RELEASE</version> </dependency> </dependencies> <!-- 特別注意:必須放過該部分資源,不然不會被編譯 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
14.1.2、編寫User實體類
@Data
public class User {
private int id;
private int score;
private String name;
}
14.1.3、編寫Dao和配置檔案xml
public interface UserDao {
List<User> selectAll();
}
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ch.dao.UserDao"> <select id="selectAll" resultType="user"> select * from student; </select> </mapper>
14.1.4、編寫mybatis配置檔案
mybatis一般編寫別名和配置【個人習慣】
<?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>
<typeAlias type="com.ch.pojo.User" alias="user"/>
<package name="com.ch.pojo"/>
</typeAliases>
</configuration>
14.1.5、編寫jdbc.properties
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mysqlstudy?serverTimezone=GMT&useSSL=true
mysql.username=root
mysql.password=root
14.1.6、編寫spring-mybatis.xml
整合還需要一個Dao的實現類
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入外部屬性檔案 -->
<context:property-placeholder location="jdbc.properties"/>
<!-- 配置資料來源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="${mysql.driver}"/>
<property name="jdbcUrl" value="${mysql.url}"/>
<property name="user" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!-- <property name="driverClassName" value="${mysql.driver}"/>-->
<!-- <property name="url" value="${mysql.url}"/>-->
<!-- <property name="username" value="${mysql.username}"/>-->
<!-- <property name="password" value="${mysql.password}"/>-->
<!-- </bean>-->
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 關聯mybatis配置檔案 -->
<property name="configLocation" value="mybatis-config.xml"/>
<!-- 新增對映器 -->
<property name="mapperLocations" value="classpath:com/ch/dao/*.xml"/>
</bean>
<!-- 通過配置好的SQLSessionFactory配置SqlSessionTemplate -->
<!-- 沒有set方法,只能通過構造器注入 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 注入實現類 -->
<bean id="userDaoImpl" class="com.ch.dao.impl.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>
14.1.7、編寫實現類
public class UserDaoImpl implements UserDao {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> selectAll() {
List<User> users = sqlSessionTemplate.getMapper(UserDao.class).selectAll();
return users;
}
}
14.1.8、測試
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserDao userDao = context.getBean("userDaoImpl", UserDao.class);
List<User> users = userDao.selectAll();
for (User user : users) {
System.out.println(user);
}
}
常見問題
1、缺少spring-jdbc
使用jdbc資料來源
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
使用c3p0資料來源
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1732)
... 35 more
2、未放過java下的.xml配置檔案
org.apache.ibatis.binding.BindingException: Type interface com.ch.dao.UserDao is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:745)
at org.mybatis.spring.SqlSessionTemplate.getMapper(SqlSessionTemplate.java:318)
at com.ch.dao.impl.UserDaoImpl.selectAll(UserDaoImpl.java:19)
at MyTest.test(MyTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
解決
2、使用SqlSessionDaoSupport
方式2直接跳過SqlSessionTemplate,使用SQLSessionFactory來實現
修改的地方如下:
1、spring-mybatis.xml
2、實現類