Spring整合Mybatis的一般步驟(IDEA版)
Spring整合Mybatis的一般步驟(IDEA版)
-
方式一
- 先在專案的pop.xml檔案下新增相關依賴(spring-wewbmvc、aspectjrt、aspectjweaver、junit、mysql-connector-java、mybatis、spring-jdbc、mybatis-spring)
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</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.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
注:若對應介面的xml檔案未放置在resources資料夾下,還需要新增以下內容,使啟動時可以訪問其他目錄下的xml檔案。
<build>
<resources>
<resource>
<directory>
src/main/resources
</directory>
<includes>
<include>
**/*.properties
</include>
<include>
**/*.xml
</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
- 在專案的resources包下,建立spring-dao.xml檔案(建立spring提供的datasource來獲取資料庫的資料來源、通過資料來源獲取sqlsessionfactory、再通過sqlsessionfactory獲取到sqlsessionTemplate、註冊對應的介面等)和mybatis.xml(設定日誌、起別名、指定介面對應的xml問價位置等)
- 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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--<property name="mapperLocations" value="classpath:com/whut/Mapper/*.xml"/>-->
</bean>
<bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="com.whut.Mapper.UserMapperImpl">
<property name="sqlsession" ref="sqlsession"/>
</bean>
</beans>
- 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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="com.whut.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/whut/Mapper/UserMapper.xml"/>
</mappers>
</configuration>
- 在Mapper(介面包)下編寫對應的xml檔案(需要在mybatis-config.xml中指定資源位置)和介面的實現類
-UserMapper介面
public interface UserMapper {
List<User> select();
}
-UserMapper的實現類
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlsession;//在spring_dao.xml中通過SqlSessionFactory獲取
public void setSqlsession(SqlSessionTemplate sqlsession) {
this.sqlsession = sqlsession;
}
public List<User> select() {
UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
return userMapper.select();
}
}
-UserMapper對應的xml檔案
<mapper namespace="com.whut.Mapper.UserMapper">
<select id="select" resultType="com.whut.pojo.User">
select * from user;
</select>
</mapper>
- 編寫測試類進行測試
@Test
public void testselect(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring_dao.xml");
UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
List<User> userList = userMapper.select();
for (User user : userList) {
System.out.println(user);
}
}
-
方式二
是對方式一的簡化,方式一在獲取sqlsession時需要先在實現類中定義私有變數sqlsession,然後設定set方法,再通過spring-dap.xml中注入來獲取到sqlsession。
方式二直接在建立實現類時通過繼承SqlsessionDaoSupport來獲取到sqlsession方法(getsession()),然後再spring-dao.xml中注入,直接通過sqlsessionfactory來得到sqlsession物件!- 方式二下的介面實現類寫法
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
public List<User> select() {
return getSqlSession().getMapper(UserMapper.class).select();
}
}
相關文章
- spring整合mybatis(idea版)SpringMyBatisIdea
- Spring6 對 整合MyBatis 開發運用(附有詳細的操作步驟)SpringMyBatis
- spring:spring與mybatis的整合SpringMyBatis
- mybatis整合springMyBatisSpring
- Spring 整合 MyBatisSpringMyBatis
- Spring整合MyBatisSpringMyBatis
- SSM整合步驟SSM
- MyBatis(九) 整合Spring、整合SpringMVCMyBatisSpringMVC
- Spring整合Mybatis plusSpringMyBatis
- Mybatis與Spring整合MyBatisSpring
- Spring整合JUnit,MybatisSpringMyBatis
- Spring Boot 整合 MyBatisSpring BootMyBatis
- Mybatis 的使用(整合Spring、SpringBoot)MyBatisSpring Boot
- Spring+SpringMVC+MyBatis的整合SpringMVCMyBatis
- MyBatis和Spring整合的奧祕MyBatisSpring
- SSM整合(Spring、SpringMVC、Mybatis)SSMSpringMVCMyBatis
- SSM——Spring整合SpringMVC,MyBatisSSMSpringMVCMyBatis
- SSM(SpringMVC + Spring + Mybatis)整合SSMSpringMVCMyBatis
- IDEA配置SSM(Spring-Spring MVC-MyBatisIdeaSSMSpringMVCMyBatis
- MyBatis的逆向工程詳細步驟操作MyBatis
- 學習C#的一般性步驟C#
- Spring框架(五)實戰Spring整合MybatisSpring框架MyBatis
- 【從零開始 圖文詳解】IDEA整合SSM框架:Spring+SpringMVC+MybatisIdeaSSM框架SpringMVCMyBatis
- Spring整合Mybatis方式二 - SqlSessionTemplateSpringMyBatisSQLSession
- Spring Boot —— 整合 MyBatis-PlusSpring BootMyBatis
- Spring+SpringMVC+MyBatis框架整合SpringMVCMyBatis框架
- Spring筆記(四)整合MybatisSpring筆記MyBatis
- JAVA學習Spring整合MybatisJavaSpringMyBatis
- SQL隱碼攻擊的原理及一般步驟SQL
- spring 程式開發步驟Spring
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring同時整合JPA與MybatisSpringMyBatis
- mybatis與spring整合ssm01MyBatisSpringSSM
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- 【Spring Boot】快速整合Mybatis-PlusSpring BootMyBatis
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- 手寫Mybatis和Spring整合簡單版示例窺探Spring的強大擴充套件能力MyBatisSpring套件
- IDEA SpringBoot專案配置熱更新的步驟詳解IdeaSpring Boot