maven+thymeleaf+Mybatis+mvc
(1)创建数据库student
(搭建配置文件)目录结构
pom.xml中
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
<scope>compile</scope>
</dependency>
</dependencies>
(2) 搭建mybatis-config.xml配置文件用来映射数据库
<?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>
<!-- 引用db.properties配置文件 -->
<properties resource="db.properties"/>
<!--
development : 开发模式
work : 工作模式
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<!-- value属性值引用db.properties配置文件中配置的值 -->
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载前面编写的SQL语句的文件 -->
<mapper resource="Mapper.xml"/>
</mappers>
</configuration>
还需要创建一个实体映射类
package com.shi;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession连接
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
}
(3)发现上面导入是<properties resource="db.properties"/>
<property name="url" value="${db.url}" /> 发现注入文件没有,创建一下db.properties
db.url=jdbc:mysql://localhost:3306/jdbc_01?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
db.user=root
db.password=root
db.driver=com.mysql.jdbc.Driver
(4)链接好数据库,去编写一下sql语局 mapper.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.shi.UserMapper">
<select id="selectUser" resultType="com.shi.Student">
SELECT * FROM student;
</select>
</mapper>
注意:id为自定义 resultType为映射类路径 创建包Student类 (有参无参构造,set,get tostring)
package com.shi;
public class Student {
private Integer id;
private String name;
private Integer age;
private String height;
private String wight;
public Student() {
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setHeight(String height) {
this.height = height;
}
public void setWight(String wight) {
this.wight = wight;
}
public Student(Integer id, String name, Integer age, String height, String wight) {
this.id = id;
this.name = name;
this.age = age;
this.height = height;
this.wight = wight;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getHeight() {
return height;
}
public String getWight() {
return wight;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", height='" + height + '\'' +
", wight='" + wight + '\'' +
'}';
}
}
(5) 配置好配置文件接下来编写MYbatis.java 当然我们要先创建UserMapper一个接口
package com.shi;
import java.util.List;
public interface UserMapper {
List<Student> selectUser();
}
用来存放集合
package com.shi;
import com.alibaba.fastjson.JSON;
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 org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Controller
public class MyTest {
@RequestMapping("/student")
public String Student(Model model) throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<Student> list = mapper.selectUser();
session.close();
// for (Student user: list){
// System.out.println(user);
//
// }
model.addAttribute("list",list);
return "hello";
// return "hello";
// return JSON.toJSONString();
// model.addAttribute("list",list);
}
}
最后记得创建html页面就可以正常使用了
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr th:each="user : ${list}">
<td>name</td>
<td th:text="${user}"></td>
<!-- <td>年龄</td>-->
<!-- <td th:text="${user.age}"></td>-->
<!-- <td>身高</td>-->
<!-- <td th:text="${user.height}"></td>-->
<!-- <td>体重</td>-->
<!-- <td th:text="${user.weight}"></td>-->
</tr>
</table>
</body>
</html>