使用Spring简化MyBatis

    技术2025-12-26  13

    使用Spring 简化 MyBatis

    需要导入的包 MyBatis相关包spring 相关包spring 整合 mybatis 的包 编写 web.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" 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_3_1.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <!-- 配置文件 --> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 创建一个监听器,加载spring 配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 原本配置的 mybatis 全局配置 <?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> <environments default="default"> <environment id="default"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm?useSSL=false&amp;serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <package name="com.mak.mapper"/> </mappers> </configuration> 在 applicationContext.xml 中配置替换 mybatis 全局配置 <?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.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/ssm?useSSL=false&amp;serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 创建sqlsessionfactory --> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据源来自 dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 创建扫描器,相当于 mybatis 全局配置文件 <mapprers> 标签下的 <package> 标签 扫描 mapper 后会给对应接口创建对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 要扫描的包 --> <property name="basePackage" value="com.mak.mapper"></property> <!-- 与 factory 产生关系 --> <property name="sqlSessionFactory" ref="factory"></property> </bean> <!-- 由 spring 管理 service 实现类 --> <bean id="peopleServiceImpl" class="com.mak.service.impl.PeopleServiceImpl"> <property name="peopleMapper" ref="peopleMapper"></property> </bean> </beans> 实体类 pojo public class People { private int id; private String name; // get、set 方法 } Mapper 类,必须使用接口绑定或注解方案 public interface PeopleMapper { @Select("select * from people") List<People> selAll(); } Service 实现类,需要在实现类中声明 mapper public class PeopleServiceImpl implements PeopleService{ private PeopleMapper peopleMapper; public PeopleMapper getPeopleMapper() { return peopleMapper; } public void setPeopleMapper(PeopleMapper peopleMapper) { this.peopleMapper = peopleMapper; } @Override public List<People> show() { return peopleMapper.selAll(); } } Servlet , spring无法管理 servlet ,取出 service 对象 @WebServlet("/people") public class PeopleServlet extends HttpServlet{ private PeopleService peopleService; @Override public void init() throws ServletException { // spring 和 web 整合信息都存放在 WebApplicationContextUtils 中 ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); peopleService = ac.getBean("peopleServiceImpl", PeopleServiceImpl.class); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("list", peopleService.show()); req.getRequestDispatcher("index.jsp").forward(req, resp); } } index.jsp 显示页面 <table> <tr> <td>编号</td> <td>姓名</td> </tr> <c:forEach items="${list }" var="l"> <tr> <td>${l.id }</td> <td>${l.name }</td> </tr> </c:forEach> </table> >>> 编号 姓名 1 张三 2 李四 3 mak 4 leihou
    Processed: 0.028, SQL: 9