SpringBoot集成mybatis
项目的结构如下
第一步在pom文件引入相应的jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
第二步引入对应的配置文件application.properties
spring.datasource.url=jdbc:mysql://自己的数据库地址:3306/数据苦名称
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
第三步实体类的编写(这里可以引入lombok可以省掉很多代码)
package com.zuojie;
public class StudentEntity {
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
第四步编写mapper(这里需要注意mybatis对sql的格式审查的比较严字段不能省略)
package com.zuojie;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface StudentMapper {
@Select("select * from student where name=#{name}")
StudentEntity findName(@Param("name") String name);
@Insert("insert into student(name,age) values(#{name},#{age})")
int save(@Param("name") String name, @Param("age") int age);
}
第五步编写controller
package com.zuojie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StuentController {
@Autowired(required=true)
private StudentMapper studentMapper;
@RequestMapping("/index")
@ResponseBody
public String index(){
return "success";
}
@RequestMapping("/find")
@ResponseBody
public StudentEntity getStudenrName(){
String name="zhangsan";
return studentMapper.findName(name);
}
@RequestMapping("/save")
@ResponseBody
public int save(){
String name="小二";
int age=10;
int save = studentMapper.save(name,age);
return save;
}
}
第六步编写启动类(这里需要注意@ComponentScan和@MapperScan这里两个注解都是扫描包文件后者是扫描mapper文件)
package com.zuojie;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* author:zuojie
*
*/
@ComponentScan(basePackages = "com.zuojie")
@MapperScan(basePackages = "com.zuojie")
@EnableAutoConfiguration
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
第六步测试