SpringBoot集成Mybatis和Druid连接池

1、建立一张学生表及对应的学生信息实体类

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `class_number` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2、pom.xml中加入Mybatis包、Mysql驱动包和Druid数据库连接池



	com.alibaba
	druid-spring-boot-starter
	1.1.10



	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	1.3.2



	mysql
	mysql-connector-java
	runtime

3、在resources目录下创建mapper文件夹

SpringBoot集成Mybatis和Druid连接池_第1张图片
4、编写mybatis-cfg.xml文件





    
        
        
    

5、修改application.properties文件,加入Druid配置和Mybatis配置

#修改tomcat端口为80
server.port=80
#设置Tomcat编码
server.tomcat.uri-encoding=UTF-8
#MyBatis配置
mybatis.config-location: classpath:mybatis/mybatis-cfg.xml  
mybatis.type-aliases-package=com.example.demo.pojo
mybatis.mapper-locations=classpath:/com/example/demo/mapper/*.xml
#数据库连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size = 5
spring.datasource.druid.max-active = 20
spring.datasource.druid.min-idle = 5
spring.datasource.druid.max-wait= 30000
#数据库配置
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/report_manage
spring.datasource.druid.username=root
spring.datasource.druid.password=root

6、编写Service基础类和Mapper基础类

BaseService.java

package com.example.demo.common.base.service;

import java.io.Serializable;
import com.example.demo.common.base.mapper.BaseMapper;

/**
 * 
 * @ClassName: BaseService
 * @Description:TODO(基础接口类)
 * @author: willdas
 * @date: 2018年9月16日 下午7:51:57
 * 
 * @param 
 * @param 
 */
public interface BaseService {
	void setBaseMapper(BaseMapper baseMapper);

	int deleteByPrimaryKey(ID id);

	int insert(T record);

	T selectByPrimaryKey(ID id);

	int updateByPrimaryKey(T record);
}

BaseServiceImpl.java

package com.example.demo.common.base.service.impl;

import java.io.Serializable;
import com.example.demo.common.base.mapper.BaseMapper;
import com.example.demo.common.base.service.BaseService;

/**
 * 
 * @ClassName:  BaseServiceImpl   
 * @Description:TODO(基础实现类)   
 * @author: willdas 
 * @date:   2018年9月16日 下午7:51:39   
 *   
 * @param 
 * @param 
 */
public abstract class BaseServiceImpl implements BaseService {

	private BaseMapper baseMapper;

	@Override
	public void setBaseMapper(BaseMapper baseMapper) {
		this.baseMapper = baseMapper;
	}

	@Override
	public int deleteByPrimaryKey(ID id) {
		return baseMapper.deleteByPrimaryKey(id);
	}

	@Override
	public T selectByPrimaryKey(ID id) {
		return baseMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKey(T record) {
		return baseMapper.updateByPrimaryKey(record);
	}

	@Override
	public int insert(T record) {
		return baseMapper.insert(record);
	}
}

BaseMapper.java

package com.example.demo.common.base.mapper;

import java.io.Serializable;

/**
 * 
 * @ClassName:  BaseMapper   
 * @Description:TODO(基础类Mapper)   
 * @author: willdas 
 * @date:   2018年9月16日 下午7:51:12   
 *   
 * @param 
 * @param 
 */
public interface BaseMapper {

	int deleteByPrimaryKey(ID id);

	int insert(T record);

	T selectByPrimaryKey(ID id);

	int updateByPrimaryKey(T record);
}

7、编写Student的Service、ServiceImpl、Mapper、Controller及Mapper.xml文件

StudentService.java

package com.example.demo.student.service;

import com.example.demo.common.base.service.BaseService;
import com.example.demo.pojo.Student;

/**
 * 
 * @ClassName:  StudentService   
 * @Description:TODO(学生信息Service接口)   
 * @author: willdas 
 * @date:   2018年9月16日 下午7:42:29   
 *
 */
public interface StudentService extends BaseService{
	
	/**
	 * 
	 * @Title: insert   
	 * @Description: TODO(新增学生信息)   
	 * @param: @param stu
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public int insert(Student stu); 
	/**
	 * 
	 * @Title: deletStu   
	 * @Description: TODO(删除学生信息)   
	 * @param: @param id
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public int deleteByPrimaryKey(Integer id);
	/**
	 * 
	 * @Title: updateByPrimaryKey   
	 * @Description: TODO(修改学生信息)   
	 * @param: @param stu
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public int updateByPrimaryKey(Student stu);
}

StudentServiceImpl.java

package com.example.demo.student.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.common.base.service.impl.BaseServiceImpl;
import com.example.demo.pojo.Student;
import com.example.demo.student.dao.StudentMapper;
import com.example.demo.student.service.StudentService;

/**
 * 
 * @ClassName: StudentServiceImpl
 * @Description:TODO(学生信息Service实现类)
 * @author: willdas
 * @date: 2018年9月16日 下午7:57:34
 *
 */
@Service
public class StudentServiceImpl extends BaseServiceImpl implements StudentService {

	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	public void setBaseMapper(StudentMapper studentMapper) {
		super.setBaseMapper(studentMapper);
		this.studentMapper = studentMapper;
	}

	/**
	 * 删除学生信息
	 */
	@Transactional(isolation = Isolation.DEFAULT)
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return studentMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 修改学生信息
	 */
	@Transactional(isolation = Isolation.DEFAULT)
	@Override
	public int updateByPrimaryKey(Student stu) {
		return studentMapper.updateByPrimaryKey(stu);
	}

	/**
	 * 新增学生信息
	 */
	@Transactional(isolation = Isolation.DEFAULT)
	@Override
	public int insert(Student stu) {
		return studentMapper.insert(stu);
	}
}

StudentMapper.java

package com.example.demo.student.dao;

import com.example.demo.common.base.mapper.BaseMapper;
import com.example.demo.pojo.Student;

/**
 * 
 * @ClassName:  StudentMapper   
 * @Description:TODO(学生信息Mapper类)   
 * @author: willdas 
 * @date:   2018年9月16日 下午7:55:04   
 *
 */
public interface StudentMapper extends BaseMapper{
	
}

StudentController.java

package com.example.demo.student.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.pojo.Student;
import com.example.demo.student.service.StudentService;

/**
 * 
 * @ClassName: StudentController
 * @Description:TODO(学生信息Controller)
 * @author: willdas
 * @date: 2018年9月16日 下午8:19:09
 *
 */
@RequestMapping("student")
@RestController
public class StudentController {

	@Autowired
	private StudentService studentService;

	/**
	 * 
	 * @Title: selectStuInfo 
	 * @Description: TODO(根据学生编号查询学生信息) 
	 * @param: 
	 * @param id 
	 * @param: 
	 * @return 
	 * @return: Student 
	 * @throws
	 */
	@GetMapping("selectStuInfo")
	public Student selectStuInfo(Integer id) {
		return studentService.selectByPrimaryKey(id);
	}
	/**
	 * 
	 * @Title: deleteStuInfo   
	 * @Description: TODO(根据编号删除学生信息)   
	 * @param: @param id
	 * @param: @return      
	 * @return: Student      
	 * @throws
	 */
	@GetMapping("deleteStuInfo")
	public Boolean deleteStuInfo(Integer id) {
		boolean flag = false;
		try{
			int count = studentService.deleteByPrimaryKey(id);
			if(count == 1){
				flag = true;
			}else{
				flag = false;
			}
		}catch(Exception e){
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 
	 * @Title: insert   
	 * @Description: TODO(添加学生信息)   
	 * @param: @param stu
	 * @param: @return      
	 * @return: Boolean      
	 * @throws
	 */
	@PostMapping("insert")   //POST请求方式
	public Boolean insert(Student stu){
		boolean flag = false;
		try{
			int count = studentService.insert(stu);
			if(count == 1){
				flag = true;
			}else{
				flag = false;
			}
		}catch(Exception e){
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 
	 * @Title: update   
	 * @Description: TODO(修改学生信息)   
	 * @param: @param stu
	 * @param: @return      
	 * @return: Boolean      
	 * @throws
	 */
	@PostMapping("update")   //POST请求方式
	public Boolean update(Student stu){
		boolean flag = false;
		try{
			int count = studentService.updateByPrimaryKey(stu);
			if(count == 1){
				flag = true;
			}else{
				flag = false;
			}
		}catch(Exception e){
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
}

StudentMapper.xml





	
		
		
		
		
	
	
		id, name, age, class_number
	
	
	
	
	
	    update student
	    set name = #{name,jdbcType=VARCHAR},
	      age = #{age,jdbcType=INTEGER},
	      class_number = #{classNumber,jdbcType=INTEGER}
	    where id = #{id,jdbcType=INTEGER}
  	
  	
  	
	    delete from student
	    where id = #{id,jdbcType=INTEGER}
	
	
	
	    insert into student (id, name, age, class_number)
	    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, 
	      #{classNumber,jdbcType=INTEGER})
	

8、修改 DemoApplication.java     

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement   //开启事务支持
@MapperScan(basePackages = "com.example.demo.*.dao")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

9、整体项目目录如下:

       SpringBoot集成Mybatis和Druid连接池_第2张图片

10、先Maven clean,然后Maven install,最后右键DemoApplication.java,Run As 启动程序

11、打开Postman,开始测试(这里就演示一个查询,其他的自己测试吧)

      查询:127.0.0.1/student/selectStuInfo?id=2

       SpringBoot集成Mybatis和Druid连接池_第3张图片

你可能感兴趣的:(SpringBoot)