Spring_2023_11_23_2 Spring整合mybatis----Xml 方式
Spring整合mybatis----Xml 方式
2023-11-23 15:18:12 星期四
1.依赖的引入
<!--spring基础依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!--spring连接数据库-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<!--添加mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--mybatis与spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.11.0</version>
2.配置文件
1. 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">
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
<property name="username" value="root"/>
<property name="password" value="127003"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis配置文件-->
<property name="configLocation" value="mybatis-config.xml"/>
<!--数据源的配置-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--Mapper接口的代理-->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--指定需要代理的接口-->
<property name="mapperInterface" value="com.bboy.mapper.StudentMapper"/>
<!--sqlSession工厂-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!--Service注册-->
<bean id="studentService" class="com.bboy.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"/>
</bean>
</beans>
2. 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>
<typeAliases>
<package name="com.bboy.pojo"/>
</typeAliases>
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
3.service
StudentService
package com.bboy.service;
import com.bboy.pojo.Student;
import java.util.List;
public interface StudentService {
public List<Student> listStudents();
public Student getStudentById(int id);
}
StudentServiceImpl
package com.bboy.service.impl;
import com.bboy.mapper.StudentMapper;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/23 0023 15:28:38
*/
public class StudentServiceImpl implements StudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public List<Student> listStudents() {
return studentMapper.listStudents();
}
@Override
public Student getStudentById(int id) {
return studentMapper.getStudentById(id);
}
}
4.mappeer
StudentMapper
package com.bboy.mapper;
import com.bboy.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentMapper {
public List<Student> listStudents();
public Student getStudentById(@Param("id") int id);
}
5.mapper映射文件
<?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.bboy.mapper.StudentMapper">
<select id="listStudents" resultType="Student">
select * from t_student;
</select>
<select id="getStudentById" resultType="Student">
select * from t_student where id = #{id}
</select>
</mapper>
6.测试类demo
package com.bboy.demo;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/23 0023 15:44:10
*/
public class Demo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) context.getBean("studentService");
List<Student> students = studentService.listStudents();
System.out.println(students);
Student student = studentService.getStudentById(2);
System.out.println("***************************************************************************");
System.out.println(student);
}
}
7.运行结果


浙公网安备 33010602011771号