springboot集成mybatis

熟悉yml语法格式

熟悉mysql数据库和其他链接操作工具

熟悉http resful 设计风格

会使用postman或者类似调试工具

只是入门级别,实际生产情况根据不同公司架构稍有不同。

数据库mysql5.6.0

本次演示集成mybatis注解版

二pom.xml <!-- springboot parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- web启动器集成tomcat spring-core sl4j日志 自动配置 spring常用注解等--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.1.RELEASE</version> </dependency> <!-- mybatis集成springboot --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies> 三在resource目录下创建 application.yml配置文件 server: port: 8082 #指定端口 spring: application: name: mybatis-annotation #应用名称 datasource: #数据源 url: jdbc:mysql://192.168.0.106:3306/springboot?useUnicode=true&characterEncoding=utf-8 username: root #账号 password: 123456 #密码 driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动 mybatis: configuration: map-underscore-to-camel-case: true #开启驼峰命名法 logging: level: com.youku1327.mybatis.mapper: debug #日志级别

# 四创建包结构

在这里插入图片描述

# 五 数据库建表

CREATE TABLE `tb_user` ( `usre_id` bigint(255) NOT NULL AUTO_INCREMENT COMMENT '用户id', `user_name` varchar(50) DEFAULT NULL COMMENT '用户名', `user_gender` varchar(2) DEFAULT NULL COMMENT '用户性别', `user_telephone` varchar(15) DEFAULT NULL COMMENT '用户电话', PRIMARY KEY (`usre_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 六建立用户实体 package com.youku1327.mybatis.entity; /** * @Author lsc * @Description 用户实体 * @Date 2019/9/20 23:58 * @Version 1.0 */ public class UserEntity { // 用户id private Long userId; // 用户名称 private String userName; // 用户性别 private String userGender; // 用户电话 private String userTelephone; public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserGender() { return userGender; } public void setUserGender(String userGender) { this.userGender = userGender; } public String getUserTelephone() { return userTelephone; } public void setUserTelephone(String userTelephone) { this.userTelephone = userTelephone; } }

# 七编写控制层

package com.youku1327.mybatis.controller; import com.youku1327.mybatis.entity.UserEntity; import com.youku1327.mybatis.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; /** * @Author lsc * @Description 控制层 * @Date 2019/9/20 23:55 * @Version 1.0 */ @RestController // 相当于 @Controller 注解 + @responseBody @RequestMapping("user") public class UserController { @Autowired UserService userService; @PostMapping(value = "info") public ResponseEntity<Integer> addUser(@RequestBody UserEntity userEntity){ int count = userService.addUser(userEntity); // 返回条数 return ResponseEntity.status(HttpStatus.OK).body(count); } @GetMapping("info") public ResponseEntity<UserEntity> getUsers(Long userId){ UserEntity users = userService.getUsers(userId); return ResponseEntity.ok(users); } @PutMapping("info/{userId}") public ResponseEntity<Integer> updateUser(@RequestBody UserEntity userEntity,@PathVariable Long userId){ int count = userService.updateUser(userEntity,userId); // 返回条数 return ResponseEntity.status(HttpStatus.OK).body(count); } @DeleteMapping("info/{userId}") public ResponseEntity<Integer> delUser(@PathVariable Long userId){ int count = userService.delUser(userId); return ResponseEntity.ok(count); } } 八服务层 package com.youku1327.mybatis.service.impl; import com.youku1327.mybatis.entity.UserEntity; import com.youku1327.mybatis.mapper.UserMapper; import com.youku1327.mybatis.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @Author lsc * @Description 用户服务层 * @Date 2019/9/21 0:24 * @Version 1.0 */ @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public int addUser(UserEntity userEntity) { int count = userMapper.addUser(userEntity); return count; } @Override public UserEntity getUsers(Long userId) { UserEntity users = userMapper.getUser(userId); return users; } @Override public int updateUser(UserEntity userEntity,Long userId) { int count = userMapper.updateUser(userEntity,userId); return count; } @Override public int delUser(Long userId) { int count = userMapper.delUser(userId); return count; } } 九mapepr package com.youku1327.mybatis.mapper; import com.youku1327.mybatis.entity.UserEntity; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; /** * @Author lsc * @Description * @Date 2019/9/21 0:13 * @Version 1.0 */ @Mapper @Repository public interface UserMapper { @Insert("INSERT INTO `springboot`.`tb_user`(`user_name`, `user_gender`) VALUES (#{userEntity.userName},#{userEntity.userGender})") int addUser(@Param("userEntity") UserEntity userEntity); @Select(" select * from `tb_user` where `user_id` = #{userId}") @ResultType(UserEntity.class) UserEntity getUser(Long userId); @Update("UPDATE `tb_user` SET user_telephone = #{userEntity.userTelephone} WHERE `user_id`=#{userId}") int updateUser(@Param("userEntity") UserEntity userEntity,@Param("userId") Long userId); @Delete("DELETE FROM `tb_user` WHERE `user_id`=#{userId}") int delUser(@Param("userId")Long userId); } 十启动类 package com.youku1327.mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Author lsc * @Description springboot启动类 * @Date 2019/9/21 0:49 * @Version 1.0 */ @SpringBootApplication @MapperScan("com.youku1327.mybatis.mapper") public class MybatisAnnotationApplication { public static void main(String[] args) { SpringApplication.run(MybatisAnnotationApplication.class,args); } } 十一测试

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjjsg.html