**如何用spring boot集成mybatis跟web项目入门笔记**
mybatis的操作数据库方法有两个,分为注解(较为方便),还有一个就是配置文件的方法,首先来看看注解的方法
1 首先创建一个springboot项目
2 第二步勾选需要的功能,maven会自动根据所选的功能导入相应的依赖jar包
3然后填好相应的项目名称一直下一步,等idea工具自己加载完所有的jar包
4 准备一张数据库的表
5 在resources目录下的application.yml文件中填好相应的配置配置文件
src\main\resources\application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/luoerdemo?serverTimezone=GMT%2B8 # 数据库名称
username: root
password: 456
mybatis:
mapper-locations: classpath:mappers/*.xml #映射配置文件路径
6 在com.luoer.demo目录下创建好项目相关包
7 首先创建好一个实体类,变量跟数据库表单属性对应,生成get set tostring方法
package com.luoer.demo.entity;
public class User {
private int id;
private int userid;
private String password;
private String name;
@Override
public String toString() {
return "User{" +
"id=" + id +
", userid=" + userid +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
8 dao层代码,类上面增加一个@mapper的注解查询语句跟 插入语句
package com.luoer.demo.dao;
import com.luoer.demo.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface Userdao{
@Select("select*from user where id=#{id}")
public List<User> getUsreInfo(int id);
@Insert("insert into user(userid,password,name) values(#{userid},#{password},#{name})")
public int save(User user);
}
9 service相关
类上面增加@Service注解
@Autowired
Userdao userdao;加载(userdao)到spring容器
package com.luoer.demo.service;
import com.luoer.demo.dao.Userdao;
import com.luoer.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
Userdao userdao;
public List<User> userServiceget(int id){
return userdao.getUsreInfo(id);
}
public int userServicesave(User user) {
return userdao.save(user);
}
}
10 controller层相关
1)由于不需要跳转jsp页面,所有使用了@RestController(相当于@Controller跟 @ResponseBody的结合
体)注解
2)这里增加了一个判断,数据库如果存入成功会有一条数据改动,所有会返回一个1,由此来判断数据是
否存入成功
3)一般注解请求的value(@GetMapping("/save"))要跟方法名一致,这里偷懒了
package com.luoer.demo.controller;
import com.luoer.demo.entity.User;
import com.luoer.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/save")
public String userControllersava(int userid,String password, String name){
User user = new User();
user.setName(name);
user.setPassword(password);
user.setUserid(userid);
//userService.userServicesave(user);
if(userService.userServicesave(user)==1){
return "存储数据成功:"+user.toString();
}else {
return "存储数据失败";
}
}
@GetMapping("/get")
public Object userController(int id){
return userService.userServiceget(id);
}
}
11 好了我们启动DemoApplication看下结果
查找请求成功,再看一下 存储能否成功
成功了,数据库也有了
12 注解的方法成功了,我们再来看下配置文件的方法
由于之前yml配置文件中配置了
mybatis:
mapper-locations: classpath:mappers/*.xml #映射配置文件路径
属性,所有,我们只需要在resources文件下穿件一个mappers的包,springboot就能找到
所以我们创建了一个user.xm文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191003052710671.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x1b2VyMDYwMw==,size_16,color_FFFFFF,t_70)
然后写了一条简单的sql查询语句
<?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.luoer.demo.dao.Userdao">
<select id="getUsreInfo" resultType="com.luoer.demo.entity.User" parameterType="int">
select * from user where id=#{id}
</select>
</mapper>
注意namespace的工作区间一定要是dao包下的接口类
注释掉原来使用注解的方式写的sql语句再试一下吧
好了,我们再试一下
成功!!!!```