您的位置:首页 > 编程语言 > Java开发

Spring boot 六 集成 MyBatis

2017-11-07 10:26 549 查看
spring boot 集成MyBatis 非常简单:

1、添加依赖包。mysql驱动 mybatis驱动(必须大于 1.1.1版本)

2、添加 application.properties 配置。

3、在 Application.main 类 添加 @MapperScan(com.zll.*)

4、添加实体对象 Student

5、编写Student数据映射对象 StudentMapper

6、编写Student业务逻辑层 StudentService

7、编写对外控制层 StudentController。

1、添加依赖包。mysql驱动 mybatis驱动(必须大于 1.1.1版本)

<!--Spring boot jpa 依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql 依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!--mybaits 依赖包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>


2、添加 application.properties 配置。

# 数据库访问配置
# 主数据源,默认的
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driverClassName = com.mysql.jdbc.Driver

#指定数据库
spring.jpa.database=mysql
# 是否显示sql语句
spring.jpa.show-sql=true
# hibernate ddl auto (create create-drop update)
spring.jpa.hibernate.ddl-auto=update
# 选择自己的 命名策略 ImprovedNamingStrategy  (比如 orderItem >> order_item)
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy


3、在 Application.main 类 添加 @MapperScan(com.zll.*)

@SpringBootApplication
@MapperScan("com.example.demo.*")//mybaits 扫描 指定报下的所有实体和映射对象。
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}


4、添加实体对象 Student

不需要注解

public class Student {

private int id;
private String name;
private String creditHour;


5、编写Person数据映射对象 StudentMapper

public interface StudentMapper {

@Insert("insert into student(name,credit_hour) value(#{name},#{creditHour})")
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//加入该注解可以保持对象后,查看对象插入id
public int insert(Student s);

@Delete("delete from student where id=#{id}")
public int delete(int id);

@Update("update student set name=#{name},credit_hour=#{credit} where id=#{id}")
public int update(@Param("id") int id, @Param("name") String name, @Param("credit") String credit);

@Select("select * from student ")
@Results({
@Result(id = true, property = "id", column = "id", javaType = Integer.class),
@Result(property = "creditHour", column = "credit_hour", javaType = String.class),
})
public List<Student> selectAll();

}


6、编写Person业务逻辑层 StudentService

@Service
public class StudentService {

@Resource
private StudentMapper mStudentMapper;

public int insert(Student s){
mStudentMapper.insert(s);
return s.getId();
}

public int delete(int id){
return mStudentMapper.delete(id);
}

public int update(int id,Student s){
return mStudentMapper.update(id,s.getName(),s.getCreditHour());
}

public List<Student> selectAll(){
return mStudentMapper.selectAll();
}

}


7、编写对外控制层 StudentController。

@RestController
@RequestMapping("/mybaits")
public class StudentController {

@Resource
private StudentService mStudentService;

@RequestMapping("/insert")
public int insert(String name,String credit) {
return mStudentService.insert(new Student(name,credit));
}

@RequestMapping("/delete")
public int delete(int id) {
return mStudentService.delete(id);
}

@RequestMapping("/update")
public int update(int id, String name,String credit) {
return mStudentService.update(id, new Student(name,credit));
}

@RequestMapping("/selectAll")
public List<Student> selectLikeName() {
return mStudentService.selectAll();
}

}


注意容易出现的问题:

Mybatis错误:Parameter ‘XXX’ not found. Available parameters are [1, 0, param1, param2]

解决办法:

在参数上加@Param(id)

查看Mapper类 Update写法

关于更多 Spring Mybatis 注解的细节 请查看 MyBiats 注解开发系列。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-boot