spring-mybatis的整合

时间:2023-03-09 22:48:15
spring-mybatis的整合

1、导入包

spring-mybatis的整合

2、创建一个请求文件发送请求

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/student/login.html" method="post">
用户名:<input type="text" name="studentName" /> <br />
密码:<input type="text" name="studentPwd" /> <br />
<input type="submit" value="登录">
</form>
</body>
</html>

3、① 创建 web.xml 文件(核心控制器,拦截所有的请求)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc-mybatis-001</display-name>
<!-- 配置过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置核心控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载spring配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

② 创建扫描组件的文件

 <?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-4.3.xsd"> <context:component-scan base-package="cn.mgy" /> </beans>

4、创建业务层处理请求

① 创建一个StudentController

 package cn.mgy.controller;

 import javax.servlet.http.HttpServletRequest;

 import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import cn.mgy.mapper.StudentMapper;
import cn.mgy.pojo.Student; @Controller
@RequestMapping("/student")
public class StudentController { @RequestMapping("/login")
public String login(Student student,HttpServletRequest req) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-*.xml");
StudentMapper studentMapper = context.getBean(StudentMapper.class);
Student student2 = studentMapper.findByStudent(student.getStudentName(), student.getStudentPwd());
req.setAttribute("student", student2);
context.close();
return "/view/login.jsp";
}
}

② 创建一个Student实体类

 package cn.mgy.pojo;

 import java.io.Serializable;
import java.util.Date; public class Student implements Serializable { private static final long serialVersionUID = 7276888295138830869L;
private Long studentId;
private String studentName;
private String studentPwd;
private Integer studentStatus;
private Date createDate;
private String studentAccount;
public Long getStudentId() {
return studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentPwd() {
return studentPwd;
}
public void setStudentPwd(String studentPwd) {
this.studentPwd = studentPwd;
}
public Integer getStudentStatus() {
return studentStatus;
}
public void setStudentStatus(Integer studentStatus) {
this.studentStatus = studentStatus;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getStudentAccount() {
return studentAccount;
}
public void setStudentAccount(String studentAccount) {
this.studentAccount = studentAccount;
}
}

5、配置数据源(整合Mybatis配置)

分三步:① 创建会话工厂;② 创建一个扫描器,将操作接口的操作对象扫描到Spring容器;③ 配置事务代理

 <?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 配置连接池 -->
<bean name="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 四要素:-->
<!-- 驱动 -->
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<!-- 连接字符串 -->
<property name="url" value="jdbc:mysql://localhost:3306/sms?useSSL=true"/>
<!-- 用户名 -->
<property name="username" value="root"/>
<!-- 密码 -->
<property name="password" value="root"/>
</bean> <!--
如何让 Mybatis 使用 Spring 的连接池?
Mybatis 整合包的实现方式,是让 Spring 代理 Mybatis 创建会话工厂,然后实现一个扫描器,
将 Mybatis 的操作接口加到 Spring 容器里面
-->
<!-- 1. 创建 Spring 代理的会话工厂 -->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 会话工厂引用连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定别名 -->
<property name="typeAliasesPackage" value="cn.mgy.pojo"></property>
<!-- 配置映射文件,Spring 支持通配符*,表示加载 -mapper.xml 结尾映射文件 -->
<property name="mapperLocations" value="classpath:cn/mgy/mapper/xml/*-mapper.xml"></property>
<property name="configuration">
<!-- 对应 mybatis-config.xml 中的 settings 标签 -->
<bean class="org.apache.ibatis.session.Configuration">
<!-- 支持驼峰命名法 -->
<property name="mapUnderscoreToCamelCase" value="true"></property>
</bean>
</property>
</bean>
<!-- 2. 使用扫描器将操作的接口扫描到 Spring 容器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 使用会话工厂 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<!-- 扫描的接口所在的包 -->
<property name="basePackage" value="cn.mgy.mapper"></property>
</bean>
<!-- 3. Spring 支持 Mybatis 的事务代理,代理 Mybatis 的事务 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 对那个数据源使用事务代理 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 编程式事务代理,事务注解支持事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

6、创建映射接口

 package cn.mgy.mapper;

 import org.apache.ibatis.annotations.Param;

 import cn.mgy.pojo.Student;

 public interface StudentMapper {
/**
* 查询学生
* @param student
* @return
*/
Student findByStudent(@Param("studentName")Object studentName,@Param("studentPwd")Object studentPwd);
}

7、创建映射文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="cn.mgy.mapper.StudentMapper">
<select id="findByStudent" resultType="Student">
SELECT * FROM tb_student WHERE STUDENT_NAME=#{studentName} AND STUDENT_PWD=#{studentPwd}
</select>
</mapper>

附:数据库

spring-mybatis的整合