声明:本博文如存在问题,欢迎各位dalao指正!!!
任务描述
本关任务:使用SpringBoot+MyBatis编写一个通过id查询用户信息的小程序。
相关知识
MyBatis-Spring-Boot-Starter可帮助你在Spring Boot之上快速构建MyBatis应用程序。为了完成本关任务,你需要掌握:1.使用MyBatis-Spring-Boot-Starter进行整合SpringBoot + MyBatis。2.使用SpringBoot + MyBatis编写一个查询用户信息的接口
Spring整合MyBatis资料下载:
自动生成demo压缩包
1.首先引入MyBatis-Spring-Boot-Starter依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2.接下来配置application.properties来定义一个DataSource
#mysql驱动
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#mysql地址
spring.datasource.url = jdbc:mysql://localhost:3306/information_schema?useUnicode=true&cha\fracterEncoding=utf-8
#mysql用户名
spring.datasource.username = root
#mysql密码
spring.datasource.password = 123123
3.使用SpringBoot + MyBatis编写一个查询用户信息的接口
//我们先使用注解的方式创建一个mapper接口:
@Mapper
public interface DemoMapper {
@Select("select * from users where userId = #{id}")
Users selectUser(int id);
}
//Users实体类如下:
@Data
public class Users {
private int id;
private String username;
private String password;
}
//接下来让mapper像下面这样注入:
@Controller
public class DemoController {
@Autowired
DemoMapper demoMapper;
@RequestMapping("/query")
@ResponseBody
public Users query(int id) {
Users user = demoMapper.selectUser(id);
return user;
}
}
// 最后我们只需要创建一个普通的Spring启动应用程序:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
编程要求及实现
*1.在 pom.xml 里增加MyBatis-Spring-Boot-Starter依赖;
*
<!-- Begin -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- End -->
2.在application.properties定义DataSource,用户名和密码前后不要有空格,不然会导致密码错误;
logging.config=classpath:log4j.properties
spring.datasource.initialization-mode=always
/********** Begin **********/
#mysql驱动
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#mysql地址
spring.datasource.url = jdbc:mysql://localhost:3306/information_schema?useUnicode=true&cha\fracterEncoding=utf-8
#mysql用户名
spring.datasource.username =root
#mysql密码
spring.datasource.password =123123
/********** End **********/
3.在Users中创建用户信息实体类userId、userName、passWord;
package net.educoder.entity;
import javax.validation.constraints.NotBlank;
import lombok.Data;
@Data
public class TUser {
/********* Begin *********/
private int userId;
private String userName;
private String passWord;
/********* End *********/
}
4.在DemoMapper增加查询用户信息的数据库访问接口;
package net.educoder.mapper;
import net.educoder.entity.TUser;
import net.educoder.entity.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface DemoMapper {
/********* Begin *********/
@Select("select * from t_user where userId = #{id}")
TUser selectUser(int id);
/********* End *********/
}
5.在DemoController增加前端请求接口,设置请求地址为/query, 调用DemoMapper的接口获取数据库数据。
package net.educoder.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import net.educoder.entity.TUser;
import net.educoder.mapper.DemoMapper;
@Controller
public class DemoController {
/********* Begin *********/
@Autowired
DemoMapper demoMapper;
@RequestMapping("/query")
@ResponseBody
public TUser query(int id){
TUser user = demoMapper.selectUser(id);
return user;
}
/********* End *********/
}
总结:
当我们使用Spring+MyBatis进行项目整合后,给人的感受就是配置一些东西变得更加简单了,我们可以很方便的从前端发送请求到后台,并操作数据。而以前需要的Servlet,dao层等,则需要配置很多文件,需要外导入很多架包,开发起来比较麻烦。