maven+thymeleaf+Mybatis+mvc
(1)创建数据库student
(搭建配置文件)目录结构
pom.xml中
junit junit4.11 test org.springframework spring-webmvc5.3.12 javax.servlet servlet-api2.5 javax.servlet.jsp jsp-api2.2 javax.servlet jstl1.2 org.thymeleaf thymeleaf3.0.9.RELEASE org.thymeleaf thymeleaf-spring43.0.9.RELEASE mysql mysql-connector-java5.1.10 org.mybatis mybatis3.4.6 org.projectlombok lombok1.18.12 provided com.alibaba druid1.2.8 com.alibaba fastjson1.2.41 compile
(2) 搭建mybatis-config.xml配置文件用来映射数据库
还需要创建一个实体映射类
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)发现上面导入是
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
SELECT * FROM student;
注意: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 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 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页面就可以正常使用了
Title
| name |



