实验四 Spring整合Mybatis
实验目的:
- 了解Spring整合Mybatis的作用
- 学会Spring整合Mybatis
- 完成Student增删改查的功能
实验类型:
????????验证性
实验学时:????????
????????2学时
实验内容:
????????实验任务1:导入Spring整合Mybatis相关依赖
<dependencies>
????<!--spring包-->
????<dependency>
????????<groupId>org.springframework</groupId>
????????<artifactId>spring-webmvc</artifactId>
????????<version>5.2.0.RELEASE</version>
????</dependency>
????<dependency>
????????<groupId>org.springframework</groupId>
????????<artifactId>spring-tx</artifactId>
????????<version>4.3.14.RELEASE</version>
????</dependency>
????<dependency>
????????<groupId>org.springframework</groupId>
????????<artifactId>spring-jdbc</artifactId>
????????<version>4.3.14.RELEASE</version>
????</dependency>
????<dependency>
????????<groupId>mysql</groupId>
????????<artifactId>mysql-connector-java</artifactId>
????????<version>5.1.45</version>
????</dependency>
????<dependency>
????????<groupId>com.baomidou</groupId>
????????<artifactId>mybatis-plus</artifactId>
????????<version>3.3.2</version>
????</dependency>
????<!--dbcp连接池-->
????<dependency>
????????<groupId>commons-dbcp</groupId>
????????<artifactId>commons-dbcp</artifactId>
????????<version>1.4</version>
????</dependency>
????<dependency>
????????<groupId>commons-pool</groupId>
????????<artifactId>commons-pool</artifactId>
????????<version>1.6</version>
????</dependency>
</dependencies>
实验任务2:在com.hxci.pojo中创建Student类
public class Student {
????private Integer id;
????private String username;
????private String sno;
????public Integer getId() {
????????return id;
????}
????public void setId(Integer id) {
????????this.id = id;
????}
????public String getUsername() {
????????return username;
????}
????public void setUsername(String username) {
????????this.username = username;
????}
????public String getSno() {
????????return sno;
????}
????public void setSno(String sno) {
????????this.sno = sno;
????}
????@Override
????public String toString() {
????????return "Student{" +
????????????????"id=" + id +
????????????????", username='" + username + '\'' +
????????????????", sno='" + sno + '\'' +
????????????????'}';
????}
}
实验任务3:在com.hxci.dao中创建接口类
public interface StudentDao {
????
????public List<Student> query();
????
????public void add(Student student);
????
????public void update(Student student);
????
????public void delete(Integer id);
????
}
实验任务4:resources下的mapper中创建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.hxci.dao.StudentDao">
????<select id="query" resultType="com.hxci.pojo.Student" >
????????select * from student;
????</select>
????<insert id="add" parameterType="com.hxci.pojo.Student">
???????insert into student(username,sno) values(#{username},#{sno});
????</insert>
????<update id="update" parameterType="com.hxci.pojo.Student">
????????update student set username=#{username},sno=#{sno} where id=#{id};
????</update>
????<delete id="delete" parameterType="int">
????????delete from student where id=#{id};
????</delete>
????<select id="queryById" resultType="com.hxci.pojo.Student">
????????select * from student where id=#{id};
????</select>
</mapper>
实验任务5:创建spring-mybaits.xml文件spring整合Mybatis
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
???????xmlns:context="http://www.springframework.org/schema/context"
???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
???????xmlns:mvc="http://www.springframework.org/schema/mvc"
???????xmlns:tx="http://www.springframework.org/schema/tx"
???????xsi:schemaLocation="http://www.springframework.org/schema/beans
????????http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
????????http://www.springframework.org/schema/context
????????http://www.springframework.org/schema/context/spring-context-4.3.xsd
????????http://www.springframework.org/schema/mvc
????????http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
????????http://www.springframework.org/schema/tx
????????http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
????<context:component-scan base-package="com.hxci.*"/>
????<context:property-placeholder location="classpath:database.properties"/>
????<bean id="dataSource" ?class="org.apache.commons.dbcp.BasicDataSource"
??????????destroy-method="close" scope="singleton">
????????<property name="driverClassName" value="${driver}" />
????????<property name="url" value="${url}" />
????????<property name="username" value="${user}" />
????????<property name="password" value="${password}" />
????????<property name="initialSize" value="${initialSize}"/>
????????<property name="maxActive" value="${maxActive}"/>
????????<property name="maxIdle" value="${maxIdle}"/>
????????<property name="minIdle" value="${minIdle}"/>
????????<property name="maxWait" value="${maxWait}"/>
????????<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
????????<property name="removeAbandoned" value="${removeAbandoned}"/>
????</bean>
????<!--配置sqlSessionFactory-->
????<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
????????<property name="dataSource" ref="dataSource"/>
????????<!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
????????<property name="typeAliasesPackage" value="com.hxci.pojo" ></property>
????????<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
????</bean>
????<!--扫描映射文件(mapper动态代理)-->
????<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
????????<property name="basePackage" ?value="com.hxci.dao"/>
????</bean>
????<!-- 配置事务管理器 -->
????<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
????????<property name="dataSource" ref="dataSource"/>
????</bean>
????<!-- 开启注解 -->
????<!--<mvc:annotation-driven/>-->
????<!--transaction-manager="transactionManager"一定要加上,否则会报错,该配置是以事物的方式开启注解-->
????<!--启用他事务注解就可以运行了-->
????<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
实验任务6:在com.hxci.service中创建service接口
public interface StudentService {
????public List<Student> query();
????public void add(Student student);
????public void update(Student student);
????public void delete(Integer id);
}
实验任务7:在com.hxci.service.impl中创建service接口的实现类
@Service
public class StudentServiceImpl implements StudentService {
????
????@Autowired
????StudentDao dao;
????
????public List<Student> query() {
????????return dao.query();
????}
????public void add(Student student) {
????????dao.add(student);
????}
????public void update(Student student) {
????????dao.update(student);
????}
????public void delete(Integer id) {
????????dao.delete(id);
????}
}
实验任务8:在com.hxci.controller中创建StudentController
@Controller
public class StudentController {
????@Autowired
????StudentService service;
????public String query(){
????????List<Student> list= service.query();
????????System.out.println(list);
????????return null;
????}
????public String add(Student student){
????????service.add(student);
????????return null;
????}
????public String update(Student student){
????????service.update(student);
????????return null;
????}
????public String delete(Integer id){
????????service.delete(id);
????????return null;
????}
}
实验任务9:创建测试类
public class MyTest {
????@Test
????public void query(){
????????ApplicationContext context=new ClassPathXmlApplicationContext("spring-mybaits.xml");
????????StudentController controller=context.getBean("studentController",StudentController.class);
????????controller.query();
????}
}
测试后成功
总结:spring整合mybatis后将sqlSessionFactory在配置文件中赋值,舍去了要经常获取Sqlsession在清除的步骤,更加简化了开发。?
|