SpringBoot第三篇SpringBoo和Mybatis整合
作者:互联网
1、新建springboot项目,选择mysql、jdbc、web,使用maven管理项目
2、检查pom.xml依赖,已经添加了mybatis、spring-boot、junit依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.yinmingneng</groupId> <artifactId>springboot1028001</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot1028001</name> <description>springboot1028001</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3、编写映射实体,user
public class user { private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } private String user_id; private String user_name; private String password; private String email; }
4、编写仓储接口,一定要写成接口,他自动生成实现类
@Repository public interface UserMapper { List<user> GetUserById(); }
5、编写xml文件,定义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="cn.yinmingneng.springboot1028001.Mapper.UserMapper"> <select id="GetUserById" resultType="cn.yinmingneng.springboot1028001.Entities.user"> select * from user </select> </mapper>
xml文件放在resources文件夹下的mapping文件下下
6、应用程序启动配置,我使用了yml文件,读起来方便,一定要配置spring的daatasource否则会报错,mybatis的获取xml文件的路径,
typeAliasesPackage:类的别名
server: port: 8095 Spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/5555?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:Mapping/*.xml //typeAliasesPackage:cn.yinmingneng.springboot1028001.Mapper
7、启动程序配置扫描包,如果不配置扫描包的路径会找不到这些接口
@MapperScan("cn.yinmingneng.springboot1028001.Mapper")
Field userMapper in cn.yinmingneng.springboot1028001.Services.UserServices required a bean of type 'cn.yinmingneng.springboot1028001.Mapper.UserMapper' that could not be found.
8、测试、启动测试
标签:SpringBoo,SpringBoot,spring,boot,id,user,Mybatis,public,String 来源: https://www.cnblogs.com/topguntopgun/p/15475660.html