1.导入MyBatis和Spring及相关jar
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.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>
3.编写Spring配置文件applicationContext.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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!--创建SqlSessionFactory对象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--扫描器相当于mybatis.xml中mappers下package标签,
扫描com.ouc.mapper包后会给对应接口创建对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--设置要扫描的包-->
<property name="basePackage" value="com.ouc.mapper"></property>
<!--和factory产生关系-->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<!--由Spring管理service实现类-->
<bean id="peopleService" class="com.ouc.service.impl.PeopleServiceImpl">
<property name="peopleMapper" ref="peopleMapper"></property>
</bean>
</beans>
4.pojo层
package com.ouc.pojo;
public class People {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public People() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
5.mapper层
【注意】必须要有接口,使用接口绑定方案或者注解方案
(1)PeopleMapper.java
package com.ouc.mapper;
import java.util.List;
import com.ouc.pojo.People;
public interface PeopleMapper {
List<People> selAll();
}
(2)PeopleMapper.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="com.ouc.mapper.PeopleMapper">
<select id="selAll" resultType="com.ouc.pojo.People">
select * from people
</select>
</mapper>
6.Service接口和Service实现类
(1)Service接口
package com.ouc.service;
import java.util.List;
import com.ouc.pojo.People;
public interface PeopleService {
List<People> show();
}
(2)Service实现类
【注意】需要在实现类中声明Mapper接口对象,并生成set/get方=法。
package com.ouc.service.impl;
import java.util.List;
import com.ouc.mapper.PeopleMapper;
import com.ouc.pojo.People;
import com.ouc.service.PeopleService;
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();
}
}
7.Servlet层
package com.ouc.servlet;
import java.io.IOException;
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 org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.ouc.service.PeopleService;
import com.ouc.service.impl.PeopleServiceImpl;
@WebServlet("/demo")
public class PeopleServlet extends HttpServlet{
private PeopleService peopleService;
@Override
public void init() throws ServletException {
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
peopleService = ac.getBean("peopleService",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);
}
}
8.JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
<table>
<c:forEach items="${list}" var="peo">
<tr>
<td>${peo.id}</td>
<td>${peo.name}</td>
<td>${peo.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>