springBoot是实现前后台分离的利器,其“约定大于配置”的思想带给我们快速开发的非凡体验,结合网上教程和自己搭建过程,将一个简单的整合分享如下:
按照我的傻瓜式介绍来,几分钟你也会顺利跑起来一个简单的springboot+mybatis的的后台。
第一步搭建项目骨架:
这是大家常用的三层结构,创建好。后面粘代码。
第二步:配置数据源application.properties
#mybatis
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crm?useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis.typeAliasesPackage=com.example.entity
mybatis.mapper-locations=classpath:mapper/*.xml
第三步:各层代码 我们从controller开始
1、controller
package com.example.controller;
import com.example.entity.User;
import com.example.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private final Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired
private TestService testService;
@RequestMapping("/getUser")
private User getUser() {
User user = testService.getUser("张三");
logger.info("查询人员");
return user;
}
}
2、service
package com.example.service;
import com.example.entity.User;
public interface TestService {
User getUser(String name);
}
package com.example.service.impl;
import com.example.dao.TestDao;
import com.example.entity.User;
import com.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestDao testDao;
@Override
public User getUser(String name) {
return testDao.getUser(name);
}
}
3、dao
package com.example.dao;
import com.example.entity.User;
public interface TestDao {
User getUser(String name);
}
4、UserMapper.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">
<mapper namespace="com.example.dao.TestDao">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<result column="name" jdbcType="VARCHAR" property="name"></result>
<result column="age" jdbcType="INTEGER" property="age"></result>
</resultMap>
<select id="getUser" parameterType="string" resultMap="BaseResultMap">
select * from user where name = #{name}
</select>
</mapper>
4、User类
package com.example.entity;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 3092592238015885727L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
5、启动类
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.example.dao")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
好了,跑去吧