标签:use ati osi 配置 ping tps jdb pac exp
1、导入Mybatis包 使用maven
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
2、配置数据库连接
注意:classpath后不需要加 /
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
3、测试
4、建立实体类,可使用Lombok来简化
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 + ‘\‘‘ + ‘}‘; } }
5、创建mapper目录以及对应的 Mapper 接口
UserMapper
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<User> queryAll(); User queeryUserById(@Param("id") int id); int deleteById(@Param("id") int id); int insert(User users); int update(User users); }
6、对应的Mapper映射文件
注意:xml文件中若要写注解,使用中文需注意,可能会报错
可将 encoding="UTF-8" 改为 encoding="gb2312"
UserMapper.xml
<?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>
7、编写部门的UserController 进行测试!
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<User> userAll(){ List<User> users = userMapper.queryAll(); for (User user : users) { System.out.println(user); } return users; } //测试项目是否能正常启动 @RequestMapping("/userl") public String user(){ return "jjjjj"; } }
标签:use ati osi 配置 ping tps jdb pac exp
原文地址:https://www.cnblogs.com/wdsjg/p/13592824.html