Mybatis查询所有

前期准备

准备数据库

最终结果:

sql语句:

1
2
3
4
5
6
7
8
create table people(
id int(10) primary key auto_increment comment '编号',
name varchar(20) comment '姓名',
age int(3) comment '年龄'
)comment '人员信息表';

insert into people values(default,'张三',21);
insert into people values(default,'李四',22);

准备实体类People

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

public class People {

private int id; //和数据库的属性匹配
private String name;
private int age;

public int getId() { //生成所有的set和get方法
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;
}
}

搭建环境

  • *1. 导入mybatis的jar包 + 依赖包 + mysql的jar包 + jstl的jar包 *

  • 2. 搭建全局配置文件

    1. 中文chm文档dtd文件找约束
    2. 写configuration内容 -- 配置数据库
    3. 写mappers的配置 -- 找PeopleMapper.xml(类似于实现类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="njdxrjgc7777777."/>
</dataSource>
</environment>
</environments>

<!--第三步-->
<mappers>
<mapper resource="com/bjstx/mapper/PeopleMapper.xml"/>
</mappers>

</configuration>
  • 3. 搭建类似于实现类的xml文件

    1. 中文chm文档dtd文件找约束
    2. 写mapper配置 -- 里面写sq语句和返回值和方法名等信息
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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.bjsxt.mapper.PeopleMapper" >
<select id="selAll" resultType="com.bjsxt.pojo.People">
select * from people
</select>
</mapper>

准备service层和实现层

PeopleService接口写方法

1
2
3
4
5
public interface PeopleService {

//显示人员
List<People> show() throws IOException;
}

PeopleServiceImpl类实现PeopleService接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* 数据访问层 -- 处理异常和控制器中处理异常 service只是抛出异常
*/
public class PeopleServiceImpl implements PeopleService{

@Override
public List<People> show() throws IOException {
// 1. 找主配置文件
InputStream is = Resources.getResourceAsStream("mybatis.xml");

// 2. 使用工厂模式 实例化工厂对象使用构建者模式 (名称标志后面:Builder)
//构建者设计模式意义:简化对象实例化过程 (我们这个SqlSessionFactory里面还要很多类很多方法比较麻烦)
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);

// 3. 创建session
SqlSession session=factory.openSession();

//4. 调用xml配置文件中的功能
List<People> list = session.selectList("com.bjsxt.mapper.PeopleMapper.setAll");

//5. 关闭连接
session.close();

return list;
}

}

准备Servlet层

1
2
3
4
5
6
7
8
9
10
11
12
13
@WebServlet("/abc/b/show")  //1.是给默认属性赋值 省略 value=  2.数组是一个值 省略 {}
public class ShowServlet extends HttpServlet {
//面向接口编程
private PeopleService peopleService=new PeopleServiceImpl();

@Override
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
List<People> list = peopleService.show();
req.setAttribute("list", list);
//使用请求转发 这次是相对路径
req.getRequestDispatcher("/index.jsp").forward(req, resp); // "/" 全路径(对WebContent目录) "" 相对路径
}
}

index.jsp首页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ 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>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<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>

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 前期准备
    1. 1.1. 准备数据库
    2. 1.2. 准备实体类People
    3. 1.3. 搭建环境
  2. 2. 准备service层和实现层
  3. 3. 准备Servlet层
  4. 4. index.jsp首页
,