SpringBoot 操作集成Mybatis 和 JPA接口.
1pom 文件:
<!-- 数据库相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<!-- mybatis 分页使用-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2此处使用Mybatis注解来实现, 新建mapper包, 然后实体是TLabel, mapper是是LabelMapper, 使用注解来实现数据库查找
package com.kason.kasonpro.entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
*
* @author hzc 2017年2月12日
*
*/
@Table(name = "t_label")
public class TLabel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Date createtime;
private String status;
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 Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "TLabel{" +
"id=" + id +
", name='" + name + '\'' +
", createtime=" + createtime +
", status='" + status + '\'' +
'}';
}
}
import com.kason.kasonpro.entity.TLabel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
/**
* Created by zhangkai12 on 2018/6/21.
*/
@Mapper
@Component(value = "labelMapper")
public interface LabelMapper {
//使用注解的方式操作mybatis
@Select("select * from t_label where id = #{id}")
public TLabel getLabel(long id);
}
3之后配置SpringBoot启动类, 扫描mapper所在的包:
@SpringBootApplication
@MapperScan("com.kason.kasonpro.mapper")
@ComponentScan
@RestController
public class KasonProApplication {
public static void main(String[] args) {
SpringApplication.run(KasonProApplication.class, args);
}
@RequestMapping("/hello")
public String hello() {
return "Hello";
}
}
此处就是@MapperScan("com.kason.kasonpro.mapper")
4编写Service
package com.kason.kasonpro.service;
import com.kason.kasonpro.entity.TLabel;
/**
* Created by zhangkai12 on 2018/6/21.
*/
public interface LabelService {
public TLabel getLabel(long id);
}
其接口实现为:
package com.kason.kasonpro.service.impl;
import com.kason.kasonpro.entity.TLabel;
import com.kason.kasonpro.mapper.LabelMapper;
import com.kason.kasonpro.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by zhangkai12 on 2018/6/21.
*/
@Service
public class LabelServiceImpl implements LabelService {
@Autowired
LabelMapper labelMapper;
@Override
public TLabel getLabel(long id) {
return labelMapper.getLabel(id);
}
}
测试类:
public class LabelServiceImplTest extends KasonProApplicationTests {
@Autowired
LabelServiceImpl labelService;
@Test
public void getLabel() throws Exception {
TLabel label = labelService.getLabel(10);
System.out.println("Label Message " + label);
Assert.assertEquals("户外",label.getName());
}
}
5使用JPA
定义好实体类 以及 相应的Repository.
实体类是
@Entity
@Table(name = "t_label")
public class TLabel implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Date createtime;
@Column(name = "status")
private Integer status;
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 Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "TLabel{" +
"id=" + id +
", name='" + name + '\'' +
", createtime=" + createtime +
", status=" + status +
'}';
}
}
LabelRepository是
package com.kason.kasonpro.repository;
import com.kason.kasonpro.entity.TLabel;
import org.springframework.data.repository.CrudRepository;
/**
* Created by zhangkai12 on 2018/6/21.
*/
public interface LabelRepository extends CrudRepository<TLabel,Integer> {
// 获取单个
public TLabel getTLabelById(int id);
}
因此Service类修改为
@Service
public class LabelServiceImpl implements LabelService {
@Autowired
LabelMapper labelMapper;
@Autowired
LabelRepository labelRepository;
@Override
public TLabel getLabel(int id) {
return labelMapper.getLabel(id);
}
@Override
public TLabel getLabelWithJPA(int id) {
return labelRepository.getTLabelById(id);
}
}
就是1引入
@Autowired
LabelRepository labelRepository;
2 即可调用相关操作.
网友评论