spring boot+mybatis示例

阅读更多

本文以简单示例展示spring boot与mybatis的整合过程:

(一)示例的目录结构


spring boot+mybatis示例_第1张图片
 

 

1、pom.xml


  4.0.0
  com.oschina
  spring-mybatis
  0.0.1-SNAPSHOT
  
   
    org.springframework.boot
    spring-boot-starter-parent
    1.5.10.RELEASE




	    
	        org.springframework.boot
	        spring-boot-starter-web
	    
	    
    	  
		
		    mysql
		    mysql-connector-java
		

          
            org.mybatis.spring.boot  
            mybatis-spring-boot-starter  
            1.3.1  
          

          
            com.alibaba  
            druid  
            1.0.29  
          


  
      
          
              
                org.springframework.boot  
                spring-boot-maven-plugin  
              
          
      

  

 

2、application.properties

server.port=8089
server.context-path=/person
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# \u521D\u59CB\u5316\u5927\u5C0F\uFF0C\u6700\u5C0F\uFF0C\u6700\u5927    
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# \u914D\u7F6E\u83B7\u53D6\u8FDE\u63A5\u7B49\u5F85\u8D85\u65F6\u7684\u65F6\u95F4    
spring.datasource.maxWait=60000

mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.oschina.entity

 

3、启动类

package com.oschina;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);
	}

}

 

4、controller层

package com.oschina.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.oschina.entity.Person;
import com.oschina.server.IPersonService;

@RestController
public class PersonController {

	@Autowired
	private IPersonService personService;

	@GetMapping("/one/{id}")
	public Person findOne(@PathVariable("id") Integer id) {
		return personService.findPersonById(id);
	}

	@GetMapping("/many")
	public List findAll() {
		return personService.findAll();
	}

	@PostMapping("/insert")
	public void insert(@RequestParam("name") String name, @RequestParam("age") Integer age) {
		personService.insertPerson(name, age);
	}

	@PutMapping("/update/{id}")
	public void update(@PathVariable("id") Integer id, @RequestParam("name") String name,
			@RequestParam("age") Integer age) {
		personService.updatePerson(id, name, age);
	}

	@DeleteMapping("/delete/{id}")
	public void delete(@PathVariable("id") Integer id) {
		personService.deletePerson(id);
	}

}

 

5、service层

package com.oschina.server;

import java.util.List;

import com.oschina.entity.Person;

public interface IPersonService {

	Person findPersonById(Integer id);

	List findAll();

	void insertPerson(String name, Integer age);

	void updatePerson(Integer id, String name, Integer age);

	void deletePerson(Integer id);
}

 

package com.oschina.server;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oschina.entity.Person;
import com.oschina.mapper.PersonMapper;

@Service
public class PersonService implements IPersonService {

	@Autowired
	private PersonMapper personMapper;

	@Override
	public Person findPersonById(Integer id) {
		return personMapper.findPersonById(id);
	}

	@Override
	public List findAll() {
		return personMapper.findAll();
	}

	@Override
	public void insertPerson(String name, Integer age) {
		personMapper.insertPerson(name, age);
	}

	@Override
	public void updatePerson(Integer id, String name, Integer age) {
		personMapper.updatePerson(id, name, age);
	}

	@Override
	public void deletePerson(Integer id) {
		personMapper.deletePerson(id);
	}

}

 

6、mapper层

package com.oschina.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.oschina.entity.Person;

@Mapper
public interface PersonMapper {

	Person findPersonById(@Param("id") Integer id);

	List findAll();

	void insertPerson(@Param("name") String name, @Param("age") Integer age);

	void updatePerson(@Param("id") Integer id, @Param("name") String name, @Param("age") Integer age);

	void deletePerson(@Param("id") Integer id);
}

 

7、实体类

package com.oschina.entity;

import java.io.Serializable;

public class Person implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private String name;
	private int age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	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;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}

 

8、mybatis xml的映射文件

  



    
    
    
    
    
       insert into tbl_person(name,age)  
       values(#{name},#{age})
    
    
    
      update tbl_person 
      
         name=#{name},
         age=#{age}
      
      where id=#{id}
    
    
    
    
     delete from tbl_person where id=#{id}
    

 

 

 

 

  • spring boot+mybatis示例_第2张图片
  • 大小: 19.9 KB
  • 查看图片附件

你可能感兴趣的:(spring boot+mybatis示例)