重生之 SpringBoot3 入门保姆级学习(17、整合SSM)
4、数据访问
4.1 整合 ssm
<?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> 3.3.0</ version> < relativePath/> </ parent> < groupId> com.zhong</ groupId> < artifactId> boot3-05-ssm</ artifactId> < version> 0.0.1-SNAPSHOT</ version> < name> boot3-05-ssm</ name> < description> boot3-05-ssm</ description> < properties> < java.version> 17</ java.version> </ properties> < dependencies> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-web</ artifactId> </ dependency> < dependency> < groupId> com.mysql</ groupId> < artifactId> mysql-connector-j</ artifactId> < scope> runtime</ scope> </ dependency> < dependency> < groupId> org.mybatis.spring.boot</ groupId> < artifactId> mybatis-spring-boot-starter</ artifactId> < version> 3.0.3</ version> </ dependency> < dependency> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId> < optional> true</ optional> </ 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> < configuration> < excludes> < exclude> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId> </ exclude> </ excludes> </ configuration> </ plugin> </ plugins> </ build> </ project>
spring.datasource.driver- class- name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc: mysql: //localhost: 3306/test
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper- locations=classpath: /mapper/*.xml
mybatis.configuration.map- underscore- to- camel- case=true
CREATE TABLE `t_user`
(`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '编号',`login_name` VARCHAR(200) NULL DEFAULT NULL COMMENT '用户名称' COLLATE 'utf8_general_ci', `nick_name` VARCHAR(200) NULL DEFAULT NULL COMMENT '用户昵称' COLLATE 'utf8_general_ci', `password` VARCHAR(200) NULL DEFAULT NULL COMMENT '用户密码' COLLATE 'utf8_general_ci',PRIMARY KEY (`id`)
);
insert into t_user(login_name,nick_name,password) values ('xiaozhong','小钟','123456');
insert into t_user(login_name,nick_name,password) values ('xiaowang','小王','123456');
package com. zhong. ssm. bean ; import lombok. Data ;
@Data
public class TUser { private Long id; private String loginName; private String nickName; private String password;
}
package com. zhong. ssm. bean. mapper ;
public interface UserMapper { }
按键盘 Alt + Enter 选择 [MybatisX] Generate of xml 选择当前项目的 resource 目录下的 mapper 文件夹 点击确定
快速生成:新建方法 getUserById 按键盘 Alt + Enter 直接回车
package com. zhong. ssm. bean. mapper ; import com. zhong. ssm. bean. TUser ;
import org. apache. ibatis. annotations. Param ;
public interface UserMapper { TUser getUserById ( @Param ( "id" ) Long id) ;
}
<?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 = " com.zhong.ssm.bean.mapper.UserMapper" > < select id = " getUserById" resultType = " com.zhong.ssm.bean.TUser" > select * from t_user where id=#{id}</ select>
</ mapper>
package com. zhong. ssm ; import org. mybatis. spring. annotation. MapperScan ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ; @MapperScan ( basePackages = "com.zhong.ssm.bean.mapper" )
@SpringBootApplication
public class Boot305SsmApplication { public static void main ( String [ ] args) { SpringApplication . run ( Boot305SsmApplication . class , args) ; } }
package com. zhong. ssm. controller ; import com. zhong. ssm. bean. TUser ;
import com. zhong. ssm. mapper. UserMapper ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. PathVariable ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
public class UserController { @Autowired UserMapper userMapper; @GetMapping ( "/user/{id}" ) public TUser getUser ( @PathVariable ( "id" ) Long id) { return userMapper. getUserById ( id) ; }
}
http://localhost:8080/user/1
http://localhost:8080/user/2