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进行接口测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值