MyBatis——07复习MyBatis过程设计
程序员文章站
2022-04-22 22:21:40
...
MyBatis过程设计
1.创建数据库
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,'代红2',18);
INSERT into people VALUES(DEFAULT,'代红1',18);
2.写出实体类
package com.youdian.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;
}
}
3.构建mybatis环境
- 首先导入jar包,包括mybatis相关的包,数据库的包,JSTL的包
- 创建全局配置文件,把DTD导入
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
全局配置文件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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sum"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/youdian/mapper/PeopleMapper.xml"/>
</mappers>
</configuration>
- 新建资源文件PeopleMapper.xml,导入mapper的DTD
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
资源文件内容如下:
<?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.youdian.mapper.PeopleMapper">
<select id="selAll" resultType="com.youdian.pojo.People">
select * from people
</select>
</mapper>
- 把资源文件的路径复制到全局配置文件mybatis.xml中,完成了数据访问层mapper(相当于以前的dao)
4.写出业务逻辑层(service)
- 创建接口PeopleService
package com.youdian.service;
import java.util.List;
import com.youdian.pojo.people;
public interface PeopleService {
/**
* 显示全部
* @return
*/
List<people> show();
}
- 在PeopleService的实现类PeopleServiceImpl中加载配置文件,因为mybatis默认不加载配置文件
Resources.getResourceAsStream(“xxx”)
xxx从classes开始找,在项目中是src文件夹开始
SqlSession是所有mybatis封装的对象,增删改查都在里面
package com.youdian.service.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.youdian.pojo.people;
import com.youdian.service.PeopleService;
/**
* 在数据访问层处理异常和在控制器中处理异常
* 在service中只抛出异常
*
*
*/
public class PeopleServiceImpl implements PeopleService{
public List<people> show() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
//前面是工厂 实例化工厂对象时使用的是 构建者设计模式 名称标志:后面有Builder
//构建者设计模式意义: 简化对象实例化过程
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
/* 等效为上面的一句话
XMLConfigBuilder parser = new XMLCofigBuilder(is);
Configuration cfg = parser.parse();
DefaultSqlSessionFatory factory2= new DefaultSqlSessionFatory(cfg);
**/
SqlSession session = factory.openSession();
List<people> list = session.selectList("com.youdian.mapper.PeopleMapper.selAll");
session.close();
return list;
}
}
- 在service接口类中需要抛出PeopleServiceImpl类的异常
package com.youdian.service;
import java.io.IOException;
import java.util.List;
import com.youdian.pojo.people;
public interface PeopleService {
/**
* 显示全部
* @return
*/
List<people> show() throws IOException;
}
5.控制器层
package com.youdian.servlet;
import java.io.IOException;
import java.util.List;
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 com.youdian.pojo.people;
import com.youdian.service.PeopleService;
import com.youdian.service.impl.PeopleServiceImpl;
@WebServlet("/show")
public class ShowServlet extends HttpServlet{
private PeopleService peopleService = new PeopleServiceImpl();
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);
}
}
注1:
- 大部分注解都有默认属性,如果注解中只给默认属性复制,可以省略属性名
- 否则在注解的(属性名=属性值)格式
- 如果一个属性是数组类型格式: 属性名={值,值} ,如果该数组只有一个值,可以省略大括号
- 如果类型不是基本数据类型或String而是一个类类型,语法: 属性名[email protected]类型
- 注解中@表示引用注解声明
注2:
- 只要路径中以/开头的都叫做全路径. 从项目根目录(WebContent)出发找到其他资源的过程
- 只要不以/开头都是相对路径,相对路径是从当前资源出发找到其他资源的过程
- 如果请求转发 / 表示WebContent目录
- 如果是重定向,静态资源引用,必须
<img src=""/> <a href=""> <script src=""/> css引用时
- 其中 /都表示的是tomcat的webapps文件夹根目录,即为服务器根目录。
需要——/虚拟项目名/xxx… - jsp运行原理:
.jsp->.java->.class->编译运行产生流数据,把流返回给浏览器 - 浏览器解析流后,如果发现有静态资源,再去发送异步请求
6.显示
- 引入taglib
<%@ 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>
推荐阅读