SpringBoot整合Mybatis【非注解形式开发】
SpringBoot整合Mybatis【非注解形式开发】
开发环境
SpringBoot+Maven+Mybatis+Mysql
整合步骤
1.pom.xml文件中引入依赖:
<!-- 整合Mybatis --> <!-- 引入starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> <scope>runtime</scope> </dependency> <!-- Mybatis的JDBC驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 引入第三方数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>
2.application.properties文件中添加配置信息:
#可以自动识别 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
3.src/main/resources目录下新建一个Folder->mybatis文件夹,
mybatis目录下新建一个mybatis配置信息文件->mybatis-config.xml
mybatis-config.xml中的配置信息如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 这里写配置内容 --> <settings> <!-- 配置关闭缓存 --> <setting name="cacheEnabled" value="false" /> <!-- 配置开启自动驼峰命名法 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 是否自动生成主键 --> <setting name="useGeneratedKeys" value="false"/> <!-- 配置默认的执行器。SIMPLE 就是普通的执行器;REUSE 执行器会重用预处理语句(prepared statements); BATCH 执行器将重用语句并执行批量更新。 --> <setting name="defaultExecutorType" value="REUSE"/> <!-- 事务超时时间 --> <setting name="defaultStatementTimeout" value="600"/> </settings> <typeAliases> <!-- 配置类别名 --> <typeAlias type="com.springboot20.xdclass.springboot.demo2.domain.Customer" alias="customer" /> </typeAliases> <mappers> <!-- 配置映射文件 --> <mapper resource="mapper/customer.xml" /> </mappers> </configuration>
4.application.properties文件中添加配置信息:
mybatis.config-location=classpath:/mybatis/mybatis-config.xml
加载mybatis-config.xml配置文件
一个开发DEMO
实现一个客户信息表的增、删、改、查:
1.src/main/java下面新建
domain包存放客户实体类Customer.java
controller包存放客户控制类CustomerController.java
service包存放客户服务接口(interface)类CustomerService.java
service.impl包存放服务实现类CustomerServiceImpl.java
mapper包存放持久层(interface)接口类CustomerMapper.java
2.各类中具体代码如下:
Customer.java:
package com.springboot20.xdclass.springboot.demo2.domain; import java.io.Serializable; import java.util.Date; /** * 功能描述:Customer 客户 domain 实体类 * @author linxifengbao * */ public class Customer implements Serializable{ private static final long serialVersionUID = 1L; private Integer id;//主键ID private String code;//编码 private String name;//姓名 private Date regTime;//注册时间 private Date updateTime;//更新时间 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
CustomerController.java:
注意点:加
@RestController类注解
package com.springboot20.xdclass.springboot.demo2.controller; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.springboot20.xdclass.springboot.demo2.domain.Customer; import com.springboot20.xdclass.springboot.demo2.domain.JsonData; import com.springboot20.xdclass.springboot.demo2.service.CustomerService; /** * 功能描述:Customer 客户 Controller 控制类 * @author linxifengbao * */ @RestController public class CustomerController { @Autowired private CustomerService customerService;//注入Service /** * 功能描述:添加客户 * postman接口测试: * http://localhost:8080/api/addCustomer * Headers设置Content-Type:application/json * Body传入Json格式对象数据: * { * "name":"张三", * "code":"zhangsan", * "regTime":null, * "updateTime":null * } * @param customer * @return */ @RequestMapping(value="/api/addCustomer") public Object addCustomer(@RequestBody Customer customer){ customer.setRegTime(new Date()); customer.setUpdateTime(new Date()); int id = customerService.addCustomer(customer); return id; } /** * 功能描述:根据ID获取客户信息 * postman接口测试: * http://localhost:8080/api/selectCustomerById/13 * @param id * @return */ @RequestMapping(value="/api/selectCustomerById/{id}") public Object selectCustomerById(@PathVariable int id){ Customer customer = customerService.selectCustomerById(id); return customer; } /** * 功能描述:修改客户信息 * postman接口测试: * http://localhost:8080/api/updateCustomer * Headers设置Content-Type:application/json * Body传入Json格式对象数据: * { * "id": 13, * "code": "zhangsan修改", * "name": "张三修改", * "regTime":null, * "updateTime":null * } * @param updateCustomer * @return */ @RequestMapping(value="/api/updateCustomer") public Object updateCustomer(@RequestBody Customer updateCustomer){ int id = updateCustomer.getId(); //获取客户信息用于前端数据回显 Customer customer = customerService.selectCustomerById(id); //修改客户信息 customerService.updateCustomer(updateCustomer); return null; } /** * 功能描述:根据id删除客户信息 * postman接口测试: * http://localhost:8080/api/deleteCustomerById?id=13 * @param id * @return */ @RequestMapping(value="/api/deleteCustomerById") public Object deleteCustomerById(int id){ customerService.deleteCustomerById(id); return null; } }
CustomerService.java
package com.springboot20.xdclass.springboot.demo2.service; import com.springboot20.xdclass.springboot.demo2.domain.Customer; /** * 功能描述:Customer 客户 Service 接口类 * @author linxifengbao * */ public interface CustomerService { /** * 功能描述:添加客户 * @param customer */ public int addCustomer(Customer customer); /** * 功能描述:根据ID获取客户信息 * @param id * @return */ public Customer selectCustomerById(int id); /** * 功能描述:修改客户信息 * @param updateCustomer */ public void updateCustomer(Customer updateCustomer); /** * 功能描述:根据ID删除客户信息 * @param id */ public void deleteCustomerById(int id); }
CustomerServiceImpl.java
注意点:加
@Service类注解(是在service接口实现类中加类注解)
package com.springboot20.xdclass.springboot.demo2.service.impl; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.springboot20.xdclass.springboot.demo2.domain.Customer; import com.springboot20.xdclass.springboot.demo2.mapper.CustomerMapper; import com.springboot20.xdclass.springboot.demo2.service.CustomerService; /** * 功能描述:Customer 客户 Service 接口实现类 * @author linxifengbao * */ @Service public class CustomerServiceImpl implements CustomerService{ @Autowired private CustomerMapper customerMapper;//注入Mapper /** * 功能描述:添加客户 */ @Override public int addCustomer(Customer customer) { customerMapper.insertCustomer(customer); int id = customer.getId(); return id; } /** * 功能描述:根据ID获取客户信息 */ @Override public Customer selectCustomerById(int id) { Customer customer = customerMapper.selectCustomerById(id); return customer; } /** * 功能描述:修改客户信息 */ @Override public void updateCustomer(Customer updateCustomer) { updateCustomer.setUpdateTime(new Date()); customerMapper.updateCustomer(updateCustomer); } /** * 功能描述:根据Id删除客户信息 */ @Override public void deleteCustomerById(int id) { customerMapper.deleteCustomerById(id); } }
CustomerMapper.java
注意点:加
@Mapper类注解
package com.springboot20.xdclass.springboot.demo2.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Options; import com.springboot20.xdclass.springboot.demo2.domain.Customer; /** * 功能描述:Customer 客户 Dao层 Mapper 接口类 * @author linxifengbao * */ @Mapper public interface CustomerMapper { /** * 功能描述:添加客户,添加后返回客户的ID,客户ID为自增 * 在mapper.xml的insert sql语句中添加: * useGeneratedKeys="true" keyProperty="id" * keyProperty="数据库中的主键字段名对应的实体类字段名" ;【填实体类字段名】 */ //@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id") public int insertCustomer(Customer customer); /** * 功能描述:根据ID获取客户信息 * @param id * @return */ public Customer selectCustomerById(int id); /** * 功能描述:修改客户信息 * @param updateCustomer */ public int updateCustomer(Customer updateCustomer); /** * 功能描述:根据ID删除客户信息 * @param id */ public int deleteCustomerById(int id); }
3.src/main/resources下新建mapper文件夹
mapper文件夹下存放customer.xml
该xml文件的加载是在mapper-config.xml文件中配置的
customer.xml中代码如下:
注意点:
<mapper namespace="">
<?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.springboot20.xdclass.springboot.demo2.mapper.CustomerMapper"> <insert id="insertCustomer" useGeneratedKeys="true" keyProperty="id"> insert into t_customer( name, code, reg_time, update_time ) value( #{name}, #{code}, #{regTime}, #{updateTime} ) </insert> <select id="selectCustomerById" resultType="customer" parameterType="int"> select * from t_customer where id = #{id} </select> <update id="updateCustomer"> update t_customer <set> <if test="name != null and name != ''"> name = #{name}, </if> <if test="code != null and code != ''"> code = #{code}, </if> <if test="regTime != null"> reg_time = #{regTime}, </if> <if test="updateTime != null"> update_time = #{updateTime}, </if> </set> where id = #{id} </update> <delete id="deleteCustomerById"> delete from t_customer where id = #{id} </delete> </mapper>
4.运行,结合postman进行接口测试
- 点赞
- 收藏
- 分享
- 文章举报
![](https://g.csdnimg.cn/static/user-reg-year/1x/2.png)
- springboot 整合mybatis 基于注解开发 传入对象
- SpringBoot+Mybatis(注解开发)整合实例
- SpringBoot 整合Mybatis3.x注解开发
- 全注解方式整合spring+mybatis模拟springboot整合
- SpringBoot 快速整合Mybatis(去XML化+注解进阶)
- springboot之整合mybatis-annotation(注解方式)
- Spring boot入门,整合mybatis开发案例
- SpringBoot开发详解(七)-- Mybatis整合Spring Boot
- SpringBoot入门-17(springboot集成mybatis注解形式增删查改properties配置)
- springboot整合mybatis-plus基于纯注解实现一对一(一对多)查询
- Spring Boot中使用MyBatis注解配置开发详解
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- SpringBoot整合mybatis进行快速开发
- 基于SpringBoot从零构建博客网站 - 整合lombok和mybatis-plus提高开发效率
- SpringBoot 快速整合Mybatis(去XML化+注解进阶)
- SpringBoot入门-21(springboot集成mybatis注解形式增删查改properties配置,利用@Provider实现动态SQL)
- [置顶] 基于注解的mybatis整合spring开发流程?
- Mybatis整合spring(Mapper代理形式开发dao)
- Spring boot Mybatis 整合(注解版)