spring——maven项目中spring整合mybatis
程序员文章站
2022-04-23 15:45:26
...
mybatis-spring目录
原理——spring整合mybatis就是动态代理拿到代理对象(sqlSession),使用代理对象通过dao层操作数据库,over!!!
目的——使用spring整合mybatis后,通过id查询school数据库中的student表中的字段,并打印在控制台。
注意——注意代码中的注释。
Ⅰ、jar包
pom.xml
<dependencies>
<!--引入spring核心容器.3个-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--spring-test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
Ⅱ、创建spring配置文件、mybatis主配置文件、mybatis映射配置文件
一、spring配置文件 0
src/main/Recources/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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--spring整合mybatis必须要的-->
<!--引入属性配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--初始化数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<!--spring整合mybatis,创建sqlSessionFactory-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--初始化mybatis主配置文件-->
<property name="configLocation" value="classpath:mybatisConfig.xml"></property>
<!--初始化mybatis映射配置文件-->
<property name="mapperLocations" value="classpath:studentMapper.xml"></property>
<!--批量取别名,可以写在mybatis主配置文件中-->
<property name="typeAliasesPackage" value="com.hbw.bean"></property>
</bean>
<!--使用spring创建接口代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<!--sqlSession操作数据库,但是由sqlSessionFactory生产sqlSessionFactory-->
<!--这里的sqlSessionFactoryBeanName是String类型,所以使用value属性,而不是ref-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<!--扫描dao层下的所有接口-->
<!--动态代理(目标代理对象操作数据库就是sqlSession),生产代理实例-->
<property name="basePackage" value="com.hbw.dao"></property>
</bean>
</beans>
二、mybatis主配置文件
src/main/Recources/mybatisConfig.xml
注意——这里虽然没写内容但是必须存在本来有些内容改在这里面写的,但是我写在spring配置文件中了
<?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>
三、mybatis映射配置文件
src/main/Recources/studentMapper.xml
<?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.hbw.dao.StudentDao">
<!--resultType是实体类全类名,但是我在spring中取别名了-->
<select id="getStudentById" resultType="student">
select sid,sname,sage,ssex,semail from student where sid=#{sid}
</select>
</mapper>
Ⅲ、附件
一、实体类
src/main/java/com/hbw/bean/Student.java
package com.hbw.bean;
import java.io.Serializable;
public class Student implements Serializable {
private int sid;
private String sname;
private int sage;
private String ssex;
private String semail;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public String getSemail() {
return semail;
}
public void setSemail(String semail) {
this.semail = semail;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", sage=" + sage +
", ssex='" + ssex + '\'' +
", semail='" + semail + '\'' +
'}';
}
}
二、dao层接口
不需要手动创建dao层实例,自动创建
src/main/java/com/hbw/dao/StudentDao.java
package com.hbw.dao;
import com.hbw.bean.Student;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentDao {
Student getStudentById(int sid);
}
三、测试
src/test/java/StudentTest.java
import com.hbw.dao.StudentDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class StudentTest {
@Autowired
private StudentDao studentDao;
@Test
public void test(){
System.out.println(studentDao.getStudentById(17));
}
}
推荐阅读
-
Spring Boot 整合mybatis 与 swagger2
-
Mybatis整合Spring 由于版本引起的BUG问题
-
Spring和MyBatis整合自动生成代码里面text类型遇到的坑
-
Spring Boot整合mybatis并自动生成mapper和实体实例解析
-
spring 整合 mybatis 中数据源的几种配置方式(总结篇)
-
Spring Boot+Mybatis的整合过程
-
Spring和MyBatis整合自动生成代码里面text类型遇到的坑
-
Spring Boot + Kotlin整合MyBatis的方法教程
-
详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
-
Spring Boot整合mybatis并自动生成mapper和实体实例解析