springboot的创建略
引入依赖 配置jdbc驱动依赖在项目的 pom.xml 中引入 连接数据库的依赖,示例代码如下。
org.springframework.boot
spring-boot-starter-data-jdbc
2.6.4
mysql
mysql-connector-java
8.0.13
配置mybatis依赖
在项目的 pom.xml 中引入 mybatis-spring-boot-starter 的依赖,示例代码如下。
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
配置application.properties/yml文件
- 在 Spring Boot 的配置文件(application.properties/yml)中对 mysql数据库连接进行配置示例代码如下。在 Spring Boot 的配置文件(application.properties/yml)中对 MyBatis 进行配置,例如指定 mapper.xml 的位置、实体类的位置、是否开启驼峰命名法等等,示例代码如下。
##################################### 数据源连接信息 ########################
spring:
datasource:
username: root
password: root
#MYSQL6.X版本的url 别忘了加上时区
url: jdbc:mysql://127.0.0.1:3306/hxzy?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&useTimezone=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
###################################### MyBatis 配置######################################
mybatis:
# 指定 mapper.xml 的位置
mapper-locations: classpath:mybatis/mapper
@Mapper
public interface UserMapper {
//通过用户名密码查询用户数据
User getByUserNameAndPassword(User user);
}
创建Mapper映射文件
参照配置文件 application.properties/yml中关于mapper的配置; 在resource目录下创建文件夹和映射文件;如:mybatis/mapper/UserMapper.xml,代码如下。
说明:
使用 Mapper 进行开发时,需要遵循以下规则:
mapper 映射文件中 namespace 必须与对应的 mapper 接口的完全限定名一致。mapper 映射文件中 statement 的 id 必须与 mapper 接口中的方法的方法名一致mapper 映射文件中 statement 的 parameterType 指定的类型必须与 mapper 接口中方法的参数类型一致。mapper 映射文件中 statement 的 resultType 指定的类型必须与 mapper 接口中方法的返回值类型一致。
id, user_id, user_name, password, email SELECT * FROM sys_user WHERe id =2
创建Services
在项目中 com.example.demo27.service 包中创建一个名为 UserService 的接口,代码如下。
package com.example.demo27.service;
import com.example.demo27.bean.User;
public interface UserService {
public User getByUserNameAndPassword(User user);
}
service实现类
com.example.demo27.service.impl 包中创建 UserService 接口的实现类,并使用 @Service 注解将其以组件的形式添加到容器中,代码如下。
注意: @Service("userService”) 注解容易忘记
package com.example.demo27.service.impl;
import com.example.demo27.bean.User;
import com.example.demo27.mapper.UserMapper;
import com.example.demo27.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User getByUserNameAndPassword(User user) {
User loginUser = userMapper.getByUserNameAndPassword(user);
return loginUser;
}
}
com.example.demo27.controller包下创建LoginController访问接口
package com.example.demo27.controller;
import com.example.demo27.bean.User;
import com.example.demo27.service.UserService;
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.RestController;
import javax.servlet.http.HttpSession;
import java.util.Map;
@RestController
public class LoginController {
@Autowired
UserService userService;
@RequestMapping("/user/single")
public User doLogin(User user, Map map, HttpSession session) {
//从数据库中查询用户信息
User loginUser = userService.getByUserNameAndPassword(user);
return loginUser;
}
}
测试浏览器输入访问地址
http://localhost:8080/user/single



