springboot mybatis层整合
2021-03-30 03:25
标签:use ati osi 配置 ping tps jdb pac exp 1、导入Mybatis包 使用maven 2、配置数据库连接 注意:classpath后不需要加 / 3、测试 4、建立实体类,可使用Lombok来简化 5、创建mapper目录以及对应的 Mapper 接口 UserMapper 6、对应的Mapper映射文件 注意:xml文件中若要写注解,使用中文需注意,可能会报错 可将 encoding="UTF-8" 改为 encoding="gb2312" 可根据mapper接口补充完整 UserMapper.xml 7、编写部门的UserController 进行测试! springboot mybatis层整合 标签:use ati osi 配置 ping tps jdb pac exp 原文地址:https://www.cnblogs.com/wdsjg/p/13592824.html
dependency>
groupId>org.mybatis.spring.bootgroupId>
artifactId>mybatis-spring-boot-starterartifactId>
version>2.1.1version>
dependency>
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis 整合 重点
mybatis.type-aliases-package=com.company.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
package com.company.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", pwd=‘" + pwd + ‘\‘‘ +
‘}‘;
}
}
package com.company.mapper;
import com.company.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
mapper namespace="com.company.mapper.UserMapper">
select id="queryAll" resultType="User">
select * from user
select>
mapper>package com.company.controller;
import com.company.mapper.UserMapper;
import com.company.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("/user")
public List
文章标题:springboot mybatis层整合
文章链接:http://soscw.com/index.php/essay/69802.html