目录
数据库的创建
创建数据库mybatis,以及内部表user,并填入数据
创建新模块Mybatis
在原有HelloSpringBoot的基础上创建新模块
选择依赖项MyBatis Framework,MySQL Driver,Spring Web
创建后等待解析依赖项
解析后,在新模板xml文件重新加载项目
新建目录
在com.example.mybatis下新建4个软件包controller、service、mapper、domain
(注:若新建软件包默认不在com.example.mybatis目录下,需手动拖进目录下)
在resources中新建一个目录mapper和一个yml文件
再在对应的目录文件下新建java文件,xml文件
若无xml文件模板,需先新建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="">
<!--namespace根据自己需要创建的的mapper的路径和名称填写-->
</mapper>
编写代码
在HelloController文件中输入
package com.example.mybatis.controller;
import com.example.mybatis.domain.User;
import com.example.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
@Autowired
private UserService userService;
@GetMapping("/hello")
public List<User> hello()
{
return userService.selectAllUser();
}
}
在User中输入代码
package com.example.mybatis.domain;
public class User {
public int id;
private String name;
private int age;
public int sex;
public String createTime;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
在UserMapper输入代码
package com.example.mybatis.domain;
public class User {
public int id;
private String name;
private int age;
public int sex;
public String createTime;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
在UserService输入代码
package com.example.mybatis.service;
import com.example.mybatis.domain.User;
import com.example.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> selectAllUser()
{
return userMapper.selectAllUser();
}
}
在UserMapper.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.example.mybatis.mapper.UserMapper">
<resultMap id="UserResult" type="User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="createTime" column="create_time" />
</resultMap>
<select id="selectAllUser" resultMap="UserResult">
select * from user
</select>
</mapper>
在application.yml中输入
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username:
password:
# 输入自己数据库的用户名和密码,以及在?前改为自己需要连接的数据库名
mybatis:
typeAliasesPackage: com.example.mybatis.domain
mapperLocations: classpath:mapper/*.xml
运行项目
运行效果如下