springBoot(四)整合之MyBatis整合

单独的一个springBoot好像有那么一点单调,那么现在开始玩整合,在这之前呢,先分享一个当初遇到的小问题,就是当你的启动器的与mapper使用的包路径并不为父子关系时,读取配置mapper可能会失败,我就遇到了。郁闷了老久,然后经过测试发现。

如果不同包,可以用@MapperScan("com.linge.springboot.mapper"),这个扫描器来定位mapper的包路径,就可以成功获取映射

 

 

MyBatis整合走起


第一步、加入MyBatis的启动器

 

博主不喜欢看到那个图标,所以关闭了面板,如果个人喜欢,可以直接启动的说。

@SpringBootApplication // 代表为SpringBoot应用的运行主类
public class Application {
  
   public static void main(String[] args) {
      /** 创建SpringApplication应用对象 */
      SpringApplication springApplication =
new SpringApplication(Application.class);
      /** 设置横幅模式(设置关闭) */
      springApplication.setBannerMode(Banner.Mode.OFF);
      /** 运行 */
      springApplication.run(args);
   }
}

 

第二步:添加所需依赖

<!-- 配置MyBatis启动器 -->
<dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.3.0</version>
</dependency>
<!-- 配置mysql驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 配置c3p0连接池 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

 

第三步:添加配置

我们可以参考依赖中的类来配置基本属性

参考spring-boot-autoconfigure-1.5.6.RELEASE.jar中jdbc包中属性文件类

DataSourceProperties

参考mybatis-spring-boot-autoconfigure-1.3.0.jar中属性文件类

MybatisPropertis

 

在src/main/resources 下添加application.properties 配置文件,内容如下:

application.properties

# 配置数据源
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
 
# 配置MyBatis3
# 配置类型别名扫描基础包
mybatis.typeAliasesPackage=com.linge.springboot.pojo
# 配置SQL语句映射文件
mybatis.mapperLocations=classpath:mappers/**/*Mapper.xml
# 配置核心配置文件
mybatis.configLocation=classpath:mybatis-config.xml
logging.level.com.linge=debug
//注意:其中logging.level.com.你的Mapper包=日志等级

 

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<!-- 全局的设置-->
	<settings>
		<!-- 开启缓存 -->
		<setting name="cacheEnabled" value="true"/>
		<!-- 启用延迟加载功能 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 按需要延迟加载-->
		<setting name="aggressiveLazyLoading" value="false"/>
		<!-- 开启驼峰映射 (方便自动映射) -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
</configuration>

 

 

第四步:整合开发

需求理解:

l 使用Spring Boot + Spring MVC + MyBatis实现查询所有公告

l  使用Spring Boot + Spring MVC + MyBatis + EasyUI 实现公告分页查询

 

 

1、编写pojo类

public class Notice implements Serializable {
private static final long serialVersionUID = 5679176319867604937L;
private Long id;
private String title;
private String content;
/** setter and getter method */
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

 

2、编写mapper接口

和之前的方式一样,只是多了@Mapper个注解
@Mapper:声明Mapper接口
@Mapper
public interface NoticeMapper {
    /** 查询所有公告 */
    @Select("select * from notice")
    public List<Notice> findAll();
   
    /** 统计查询 */
    public Long count();
    /** 分页查询公告 */
    public List<Notice> findByPage(@Param("page")Integer page, @Param("rows")Integer rows);
}

 

3、编写mappe.xml配置文件

<?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">  
<mapper namespace="com.linge.springboot.mapper.NoticeMapper">
 
    <!-- 统计查询 -->
    <select id="count" resultType="long">
       select count(*) from notice
    </select>
 
    <!-- 分页查询公告 -->
    <select id="findByPage" resultType="notice">
       select * from notice limit #{page},#{rows}
    </select>
</mapper>

 

4、编写Service与实现类

@Service
@Transactional
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    /** 查询所有的公告 */
    public List<Notice> findAll(){
       return noticeMapper.findAll();
    }
    /** 分页查询公告 */
    public Map<String,Object> findByPage(Integer page, Integer rows){
       /** 创建Map集合封装响应数据 */
       Map<String,Object> data = new HashMap<>();
       /** 统计查询 */
       long count = noticeMapper.count();
       data.put("total", count);
       /** 分页查询 */
       List<Notice> notices = noticeMapper.findByPage(page, rows);
       data.put("rows", notices);
       return data;
    }
}

 

5、编写controller

@Controller
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    /** 查询全部公告 */
    @GetMapping("/findAll")
    @ResponseBody
    public List<Notice> findAll(){
       return noticeService.findAll();
    }
    /** 跳转分页查询公告页面 */
    @GetMapping("/show")
    public String show(){
       return "/html/notice.html";
    }
    /** 分页查询公告 */
    @PostMapping("/findByPage")
    @ResponseBody
    public Map<String,Object> findByPage(@RequestParam(value="page",
                         defaultValue="1",required=false)Integer page,
                         @RequestParam(value="rows",
                         defaultValue="15",required=false)Integer rows){
       return noticeService.findByPage((page - 1) * rows, rows);
    }
}

 

6、加入静态资源

src/main/resources/public/html/notice.html

src/main/resources/static/js

src/main/resources/static/css

src/main/resources/static/images

 

 

 

启动测试

浏览器地址栏输入:http://localhost:8080/findAll |show

 

完成,如果有什么问题,可以一起讨论一下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值