Spring整合MyBatis
环境搭建
配置文件
1.配置Spring的AppliactionContext.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="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_contracts?serverTimezone=UTC" ></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建SqlSessionFactory对象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库连接信息来源于dataSouce-->
<property name="dataSource" ref="dataSouce"></property>
</bean>
<!--扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描哪个包下的接口-->
<property name="basePackage" value="cn.com.mapper"></property>
<!--连接factory-->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
</beans>
2.在相关包下建立接口
(1)如果使用注解,就不用使用mapper.xml的配置文件
package cn.com.mapper;
import cn.com.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> selectAll();
}
(2)如果使用mapper.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="cn.com.mapper.UserMapper">
<select id="selectAll" resultType="cn.com.pojo.User" >
select * from user
</select>
</mapper>
使用mapper.xml的好处是可以使用动态SQL
package cn.com.mapper;
import cn.com.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
List<User> selectAll();
}
3.测试
package cn.com.test;
import cn.com.mapper.UserMapper;
import cn.com.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("appliactionContext.xml");
UserMapper userMapper=ac.getBean("userMapper",UserMapper.class); //获取接口
List<User> users=userMapper.selectAll();
for (User u:users) {
System.out.println(u);
}
}
}
Spring整合MyBatis和Servlet
环境搭建
配置文件
(1)使用mapper.xml的方式
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:appliactionContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--数据源封装类-->
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_contracts?serverTimezone=UTC" ></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建SqlSessionFactory对象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库连接信息来源于dataSouce-->
<property name="dataSource" ref="dataSouce"></property>
<!-- 映射mybatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- 映射mapper文件 -->
<property name="mapperLocations">
<list>
<value>classpath:cn/com/mapper/*.xml</value>
</list>
</property>
</bean>
<!--扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描哪个包下的接口-->
<property name="basePackage" value="cn.com.mapper"></property>
<!--连接factory-->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<bean id="userService" class="cn.com.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
</beans>
3.配置mybatis.xml
这是为了配置mapper.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="cn.com.pojo"/>
</typeAliases>
</configuration>
4.编写mapper.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="cn.com.mapper.UserMapper">
<select id="selectAll" resultType="cn.com.pojo.User">
select * from user
</select>
</mapper>
5.编写mapper的接口
package cn.com.mapper;
import cn.com.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;
import java.util.List;
@MapperScan("cn.com.mapper")
public interface UserMapper {
List<User> selectAll();
}
6.编写service层
接口
package cn.com.service;
import cn.com.pojo.User;
import java.util.List;
public interface UserService {
List<User> showAll();
}
实现类
package cn.com.service.impl;
import cn.com.mapper.UserMapper;
import cn.com.pojo.User;
import cn.com.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List<User> showAll() {
return userMapper.selectAll();
}
}
7.编写servlet
package cn.com.servlet;
import cn.com.service.UserService;
import cn.com.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/abc")
public class UserServlet extends HttpServlet {
private UserService userService;
@Override
public void init() throws ServletException {
ApplicationContext ac= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
//sping的容器整合web
userService=ac.getBean("userService", UserServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("访问");
req.getRequestDispatcher("/main.jsp").forward(req,resp);
}
@Override
public void destroy() {
super.destroy();
}
}
servlet使用了注解,如果不使用注解就需要在web.xml进行配置
8.编写页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<c:forEach items="${userlist}" var="user">
${user.username} ${user.password} <br>
</c:forEach>
</body>
</html>
9.测试
(2)MyBatis使用注解的方式
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:appliactionContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--数据源封装类-->
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_contracts?serverTimezone=UTC" ></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建SqlSessionFactory对象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库连接信息来源于dataSouce-->
<property name="dataSource" ref="dataSouce"></property>
<!--
<!– 映射mybatis核心配置文件 –>
<property name="configLocation" value="classpath:mybatis.xml"/>
<!– 映射mapper文件 –>
<property name="mapperLocations">
<list>
<value>classpath:cn/com/mapper/*.xml</value>
</list>
</property> 只使用注解可以不要-->
</bean>
<!--扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描哪个包下的接口-->
<property name="basePackage" value="cn.com.mapper"></property>
<!--连接factory-->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<bean id="userService" class="cn.com.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
</beans>
3.编写mapper的接口
package cn.com.mapper;
import cn.com.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;
import java.util.List;
@Select("select * from user")
public interface UserMapper {
List<User> selectAll();
}
4.编写service层
接口
package cn.com.service;
import cn.com.pojo.User;
import java.util.List;
public interface UserService {
List<User> showAll();
}
实现类
package cn.com.service.impl;
import cn.com.mapper.UserMapper;
import cn.com.pojo.User;
import cn.com.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List<User> showAll() {
return userMapper.selectAll();
}
}
5.编写servlet
package cn.com.servlet;
import cn.com.service.UserService;
import cn.com.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/abc")
public class UserServlet extends HttpServlet {
private UserService userService;
@Override
public void init() throws ServletException {
ApplicationContext ac= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
//sping的容器整合web
userService=ac.getBean("userService", UserServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("访问");
req.setAttribute("userlist",userService.showAll());
req.getRequestDispatcher("/main.jsp").forward(req,resp);
}
@Override
public void destroy() {
super.destroy();
}
}
servlet使用了注解,如果不使用注解就需要在web.xml进行配置
6.编写页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<c:forEach items="${userlist}" var="user">
${user.username} ${user.password} <br>
</c:forEach>
</body>
</html>
7.测试