整个架构图
习惯先写entity包:存放实体类User
package com.example.demo.entity;
public class User {
private Integer id;
private String userName;
private String passWord;
private String realName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", realName='" + realName + '\'' +
'}';
}
}
在写mapper包中的UserMapper类
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User getUser(int id);
}
service包 UserService类
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User getUser(int id){
return userMapper.getUser(id);
}
}
controller包 UserController类
package com.example.demo.controller;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/testBoot/")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getUser/{id}")
public String getUser(@PathVariable int id){
return userService.getUser(id).toString();
}
}
demo 下的启动类
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在resource包下创建mapping包放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.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
<result column="realName" jdbcType="VARCHAR" property="realName" />
</resultMap>
<select id="getUser" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>
</mapper>
注意<mapper namespace="com.example.demo.mapper.UserMapper"> 要与UserMapper类路径一模一样
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">要与User类路径一模一样
<select id="getUser" resultType="com.example.demo.entity.User"> id 就是UserMapper类中的getUser方法,必须对应,不然无法绑定报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
application.yml文件内容
spring:
profiles:
active: dev
dev表示生产环境的配置,
在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
application-dev.yml 内容
server:
port: 8082
spring:
datasource:
username: 账户默认是root
password: 密码
url: jdbc:mysql://localhost:3306/edu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml 路径名称要与UserMapper.xml一样
type-aliases-package: com.example.demo.entity 路径名称要与User类一样
#showSql
logging:
level:
com:
example:
mapper : debug