一、环境
springboot:2.7.3
jdk:1.8
idea:2021.3
mysql:8.0.29
二、前期准备(连接数据库)
1.新建module,选好依赖,这里选择spring web、mybatis和mysql依赖,在pom.xml中查看结果。
2.文件框架:就是spring中的dao、service和controller层。推荐使用yml的配置文件(不会乱码)
3.Mysql数据库的创建,使用Navicat简单创建即可,名为springdb,包含id、name、age字段。
4.application.yml 文件配置, 用于连接本地的mysql数据库。这样基本就能连上数据库
# 设置端口号和访问地址orm
server:
port: 9001
servlet:
context-path: /orm
# 连接数据库, 使用cj.jdbc驱动
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# 指定使用Unicode编码, 字符集utf8, serverTimezone表示时区 GMT是标准时区, 也可以写成 serverTimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
name: root
password: 123
# 设置具体操作数据的xml目录 和运行日志
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
5.再在pom.xml中添加下resource插件, 目的是使用后续resources文件夹下mapper文件生效
<build>
<!--resource插件 使得src/main/resources的所有文件-->
<!-- 可以点击maven中的Lifecycle中的clean和compile进行刷新-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
三、业务逻辑
1.model(Bean):新建Student类,与mysql的字段相对应。包含set和get方法。
2.dao层:在dao文件下新建StudentDao的接口,具体操作建在resources.mapper.StudentDao.xml
StudentDao.interface
package com.bjpowernode.dao;
import com.bjpowernode.model.Student;
import org.apache.ibatis.annotations.Param;
public interface StudentDao {
// 使用Param做命名
Student selectById(@Param("stuId") Integer id);
}
StudentDao.xml:务必注意里面的参数设置,很容易出错
<?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">
<!--该dao接口的权限名称 必须是Dao的路径-->
<mapper namespace="com.bjpowernode.dao.StudentDao">
<!--定义sql语句,其中{}里的stuId是Dao里面定义好的命名, resultType指定好实体类的位置 -->
<select id="selectById"
resultType="com.bjpowernode.model.Student">
select id, name, age from student where id=#{stuId}
</select>
</mapper>
3.service层:创建名为StudentService的接口及其实现StudentServiceImpl
StudentService.interface
package com.bjpowernode.service;
import com.bjpowernode.model.Student;
public interface StudentService {
Student queryStudent(Integer id);
}
StudentServiceImpl:可以光标移动到上面文件的StudentService,按alt+enter点击implement会自动生成该文件。 务必注意各个注解的编写。
package com.bjpowernode.service.impl;
import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.model.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
// 声明业务对象
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public Student queryStudent(Integer id) {
Student student = studentDao.selectById(id);
return student;
}
}
4.Controller层:使用StudentService对象 设置访问地址为/student/query。
package com.bjpowernode.controller;
import com.bjpowernode.model.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("/student/query")
@ResponseBody
public String queryStudent(Integer id){
Student student = studentService.queryStudent(id);
return student.toString();
}
}
5.最后在运行文件中,加入MapperScan注解,使得容器可以找到Dao接口和mapper文件。再运行
package com.bjpowernode;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// com.bjpowernode是组名 group
@MapperScan(basePackages = {"com.bjpowernode.dao", "com.bjpowernode.mapper"})
/**
* @MapperScan: 找到Dao接口和Mapper文件
* basePackages:Dao接口所在的包名
*/
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.至此,简单的逻辑就完事了。查看运行结果