springboot整合mybatis

it2022-05-05  114

1.SpringBoot 简介 1.1 Spring Team在现有Spring框架的基础上发布了一个创新的框架:Spring Boot。 1.2 Spring Boot的开发团队是:PivotalSpring Boot的主要作用是:简化开发,减少配置(简化配置和部署spring应用程序的过程)。 1.3 Spring Boot框架提倡:一键部署、习惯优于配置Spring Boot还是一个微框架,它与目1.4 前流行的微服务紧密联系,可以开发微应用。

2.引入依赖

org.mybatis mybatis 3.4.6 mysql mysql-connector-java 5.1.44 com.mchange c3p0 0.9.5.1 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 3.配置数据源,和加载Mybatis配置 3.1.在application.properties中配置数据源

#配置数据源 spring.datasource.url=jdbc:mysql://localhost:3306/llmjsh?characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root

加载mybatis 配置

mybatis.mapper-locations=classpath:mapper/*.xml mybatis.config-location=classpath:mybatis/mybatis-config.xml 1 2 3 4 5 6 7 8 9 比如这样!!!

4.在 src/main/resources 下创建 mybatis 文件夹,并在 mybatis 文件夹中创建 “mybatis-config.xml” 配置文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

1 2 3 4 5 6 7 8 9 10 11 12 13 5.创建数据库

5.创建实体类 此处省略get,set

public class Student implements Serializable { private int uid; private String name; private String sex; private String age; private String address; } 1 2 3 4 5 6 7 6.创建dao接口 public interface Studentdao { Student studentAll();

} 1 2 3 4 7.创建Service public interface StudentService { Student studentAll(); } 1 2 8.创建ServiceImpl 实现类 @Service(“studentService”) public class StudentServiceImpl implements StudentService{

@Resource private Studentdao studentdao; @Override public Student studentAll() { return studentdao.studentAll(); }

} 1 2 3 4 5 6 7 8 9 10 11 7.创建StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?> <select id="studentAll" resultType="com.llmjsh.springboot1.pojo.Student"> select * from tb_Student </select> 1 2 3 4 5 6 7 8 9 10 8.在controller中调用 @Controller public class StudentController { @Resource private StudentServiceImpl studentService; @RequestMapping(value = "/hello",produces="text/html;charset=UTF-8") @ResponseBody public Student Test(){ Student students = studentService.studentAll(); System.out.println(students); return students; }

} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 9.运行输出结果如下

10.项目目录结构展示


作者:我们矢志不渝 来源: 原文:https://blog.csdn.net/weixin_43127338/article/details/83927830 版权声明:本文为博主原创文章,转载请附上博文链接!


最新回复(0)