栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot03、实现mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot03、实现mybatis

springboot03、实现mybatis

目录

1、pom引包

2、数据源配置application.properties

3、model对应数据库的类Users

4、dao层·需要加上@Repository注解

5、添加映射文件UsersMapper.xml

6、service层编码

7、serviceimpl层编码

8、Controller层编码

9、测试效果:


1、pom引包
	   
			org.springframework.boot
			spring-boot-starter
		
		
		
			org.springframework.boot
			spring-boot-devtools
		
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
		
			mysql
			mysql-connector-java
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.1.2
		
		
		
			junit
			junit
			test
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		

2、数据源配置application.properties

Spring Boot 会将数据源自动注入到 MyBatis 的 sqlSessionFactory 组件中。对于我们开发者来说,这一切都是自动实现的,非常简单。

# 配置数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置数据库url
spring.datasource.url=jdbc:mysql://rm-bp1zq3879r28p726lco.mysql.rds.aliyuncs.com:3306/test?characterEncoding=utf8
# 配置数据库用户名
spring.datasource.username=qwe8403000
# 配置数据库密码
spring.datasource.password=Qwe8403000
# 指定MyBatis配置文件位置
mybatis.mapper-locations=classpath:mapper
@Repository
public interface UsersMapper {
    List GetInfo();
    List SelectName(@Param("nickName") String nickName);
    Users UsersSelectById(@Param("id") int id);
    int UsersAddInfo(
            @Param("userName") String userName,
            @Param("pwd") String pwd,
            @Param("nickName") String nickName
    );
    int UsersUpdateInfo(
            @Param("pwd") String pwd,
            @Param("id") int id
    );
    int UsersDeleteById(@Param("id") int id);
}

5、添加映射文件UsersMapper.xml



    
    
        select * from users where nickName like "%${nickName}%"