1.SpringBoot构建已经Maven工程搭建
选择依赖:将Web内的Spring Web导入
2.修改配置文件:使用yml语法
将application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为*application.properties*)
application.yml
spring:
profiles:
active: dev
application-dev.yml
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mybatis/UserMapper.xml
type-aliases-package: com.example.springboot.pojo
#showSql
logging:
level:
com:
example:
mapper : debug
注意:在实际项目中通常不使用application.properties来进行配置。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml
笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
配置中最好不要出现中文,否则可能会报错
3.在resource目录中建一个包来存放Mybatis的.xml文件(写sql,详情见Mybatis)
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.springboot.mapper.UserMapper">
<select id="Sel" resultType="com.example.springboot.pojo.User">
select * from user where id = #{id}
</select>
4.业务流程: 创建包controller,pojo,mapper,service
实体类:pojo.User
package com.example.springboot.pojo;
import javax.annotation.sql.DataSourceDefinition;
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
mapper.UserMapper
package com.example.springboot.mapper;
import com.example.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface UserMapper {
User Sel(int id);
List<User> getUserList();
//查询
User getUserById(int id);
//增加
int addUser(User user);
//删除
int deleteUser(int id);
//修改
int updateUser(User user);
//万能Map
int addUser2(Map<String,Object> map);
//Map修改
int updateMapUser(Map<String,Object> map);
}
service.UserService
package com.example.springboot.Service;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User Sel(int id){
return userMapper.Sel(id);
}
}
controller.UserController
package com.example.springboot.controller;
import com.example.springboot.Service.UserService;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/testBoot")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getUser/{id}")
public String GetUser(@PathVariable int id){
return userService.Sel(id).toString();
}
}
5.完成以上 在启动类中给出需要扫描的mapper路径(添加注解@MapperScan(“url”) )
package com.example.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.springboot.mapper")//@MapperScan 用户扫描MyBatis的Mapper
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
**最后启动,浏览器输入地址:**http://localhost:8080/testBoot/getUser/1**