一、配置文件方式实现
前提:已将数据库驱动包引入,并且已配置好数据源信息
1、引入依赖
<!-- mybatis依赖包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2、添加配置信息
#mybatis配置信息
#mybatis.config-location=classpath:mybatis/mybatis-config.xml
#mapper映射文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
#配置xml文件中resultType返回值的包位置
mybatis.typeAliasesPackage=qingxia.tang.jpa.bean
3、写mapper接口类
package qingxia.tang.jpa.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import qingxia.tang.jpa.bean.Cat;
/**
* 需要使用@Mapper注解,不然SpringBoot无法扫描,或者在启动类中添加对mapper包扫描@MapperScan
* @author tangqingxia
*
*/
public interface CatMapper {
/**
* xml配置文件方式
* @return
*/
public List<Cat> queryAllCats();
}
4、编写在resources文件中创建 mapper/CatMapper.xml映射文件
注意:
1.namespace中需要与使用@Mapper的接口对应
2.CatMapper.xml文件名称必须与使用@Mapper的接口一致
3.标签中的id必须与@Mapper的接口中的方法名一致,且参数一致
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--
注意:
1.namespace中需要与使用@Mapper的接口对应
2.CatMapper.xml文件名称必须与使用@Mapper的接口一致
3.标签中的id必须与@Mapper的接口中的方法名一致,且参数一致
-->
<mapper namespace= "qingxia.tang.jpa.mapper.CatMapper" >
<resultMap id ="CatInfoMap" type="qingxia.tang.jpa.bean.Cat">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="address_id" property="addressId"/>
</resultMap>
<select id = "queryAllCats" resultMap="CatInfoMap">
select id,name,age,address_id from cat
</select>
</mapper>
5、编写service
package qingxia.tang.jpa.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.mapper.CatMapper;
@Service
public class CatMapperService {
@Autowired
private CatMapper catMapper;
public List<Cat> queryAllCats(){
List<Cat> queryAllCats = catMapper.queryAllCats();
return queryAllCats;
}
}
6、编写controller
package qingxia.tang.jpa.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.service.CatMapperService;
@Controller
public class CatMapperController {
@Autowired
private CatMapperService catMapperService;
@ResponseBody
@RequestMapping(value="/queryAllCats",produces={"application/json"})
public List<Cat> queryAllCats(){
List<Cat> queryAllCats = catMapperService.queryAllCats();
return queryAllCats;
}
}
7、在启动类app.java添加@MapperScan(“qingxia.tang.jpa.mapper”)注解,也可以在mapper接口类
添加@Mapper注解代替,但此注解需要在每个mapper接口类添加
package qingxia.tang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBootApplication注解相当于Controller注解和responseBody注解
* 在使用mybatis时,需要在启动类app.java添加@MapperScan("qingxia.tang.jpa.mapper")注解,
* 也可以在mapper接口类添加@Mapper注解代替,但此注解需要在每个mapper接口类添加
*
* @author tangqingxia
*
*/
@MapperScan("qingxia.tang.jpa.mapper")
@SpringBootApplication
public class App /** extends WebMvcConfigurerAdapter */{
/**
* 配置FastJson方式一
* 1、继承extends WebMvcConfigurerAdapter类
* 2、重写configureMessageConverters方法
*/
/**
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//定义convert的转换对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//把配置信息添加到对象中
fastConverter.setFastJsonConfig(fastJsonConfig);
//把新建的对象替换返回
converters.add(fastConverter);
}
*/
/**
* 配置FastJson方式二
* Bean注入的方式
* @return HttpMessageConverters
*/
/**
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
*/
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
8、测试
二、注解方式实现
1、引入依赖
<!-- mybatis依赖包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2、写mapper接口类
package qingxia.tang.jpa.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import qingxia.tang.jpa.bean.Cat;
/**
* 需要使用@Mapper注解,不然SpringBoot无法扫描,或者在启动类中添加对mapper包扫描@MapperScan
* @author tangqingxia
*
*/
public interface CatMapper {
/**
* 注解方式
* 如属性名和列名不一致的需加上@Result注解进行结果返回
* @return
*/
@Select("select t.id,t.name,t.age,t.address_id from cat t")
@Results({
@Result(property = "addressId", column = "address_id"),
})
public List<Cat> queryAllCatsByAnn();
}
3、编写service
package qingxia.tang.jpa.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.mapper.CatMapper;
@Service
public class CatMapperService {
@Autowired
private CatMapper catMapper;
public List<Cat> queryAllCatsByAnn(){
List<Cat> queryAllCatsByAnn = catMapper.queryAllCatsByAnn();
return queryAllCatsByAnn;
}
}
4、编写controller
package qingxia.tang.jpa.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.service.CatMapperService;
@Controller
public class CatMapperController {
@Autowired
private CatMapperService catMapperService;
@ResponseBody
@RequestMapping(value="queryAllCatsByAnn",produces={"application/json"})
public List<Cat> queryAllCatsByAnn(){
List<Cat> queryAllCatsByAnn = catMapperService.queryAllCatsByAnn();
return queryAllCatsByAnn;
}
}
5、在启动类app.java添加@MapperScan(“qingxia.tang.jpa.mapper”)注解,也可以在mapper接口类
添加@Mapper注解代替,但此注解需要在每个mapper接口类添加
package qingxia.tang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBootApplication注解相当于Controller注解和responseBody注解
* 在使用mybatis时,需要在启动类app.java添加@MapperScan("qingxia.tang.jpa.mapper")注解,
* 也可以在mapper接口类添加@Mapper注解代替,但此注解需要在每个mapper接口类添加
*
* @author tangqingxia
*
*/
@MapperScan("qingxia.tang.jpa.mapper")
@SpringBootApplication
public class App /** extends WebMvcConfigurerAdapter */{
/**
* 配置FastJson方式一
* 1、继承extends WebMvcConfigurerAdapter类
* 2、重写configureMessageConverters方法
*/
/**
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//定义convert的转换对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//把配置信息添加到对象中
fastConverter.setFastJsonConfig(fastJsonConfig);
//把新建的对象替换返回
converters.add(fastConverter);
}
*/
/**
* 配置FastJson方式二
* Bean注入的方式
* @return HttpMessageConverters
*/
/**
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
*/
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
6、测试