使用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>
<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&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&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mak.mapper"></property>
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<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;
}
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 {
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
转载请注明原文地址:https://ipadbbs.8miu.com/read-61695.html