0.项目结构:
---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现---------------------
1.application.properties中配置整合mybatis的配置文件、mybatis扫描别名的基本包与数据源
server.port=80
logging.level.org.springframework=DEBUG
#springboot mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = cn.qlq.bean
#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
2.pom.xml加入springboot整合mybatis的jar包与数据库驱动包
4.0.0
cn.qlq
springboot-ssm
0.0.1-SNAPSHOT
war
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
5.1.6
junit
junit
4.9
test
javax.servlet
servlet-api
2.5
provided
javax.servlet
jsp-api
2.0
provided
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.7
1.7
UTF-8
org.springframework.boot
spring-boot-maven-plugin
3.mybatis全局配置文件:
sqlMapConfig.xml
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
4.编写代码:
(1)User.java
packagecn.qlq.bean;importjava.io.Serializable;importjava.util.Date;public class User implementsSerializable{/****/
private static final long serialVersionUID = 1L;privateInteger id;privateString username;privateDate birthday;privateString sex;privateString address;publicInteger getId() {returnid;
}public voidsetId(Integer id) {this.id =id;
}publicDate getBirthday() {returnbirthday;
}public voidsetBirthday(Date birthday) {this.birthday =birthday;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}
}
(2)UserMapper.java (注意接口上的注解是@mapper,代替之前扫描接口的操作)
packagecn.qlq.mapper;importjava.util.List;importorg.apache.ibatis.annotations.Mapper;importcn.qlq.bean.User;
@Mapperpublic interfaceUserMapper {public ListfindAll();
}
UserMapper.xml
select * from user
(3)
UserService.java
packagecn.qlq.service;importjava.util.List;importcn.qlq.bean.User;public interfaceUserService {/*** 根据接口查询所用的用户*/
public ListfindAllUser();
}
UserServiceImpl.java
packagecn.qlq.service.impl;importjava.util.List;importjava.util.Map;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcn.qlq.bean.User;importcn.qlq.mapper.UserMapper;importcn.qlq.service.UserService;
@Servicepublic class UserServiceImpl implementsUserService {
@AutowiredprivateUserMapper userMapper;public ListfindAllUser() {
List list =userMapper.findAll();returnlist;
}
}
(4)UserController.java
packagecn.qlq.action;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importcn.qlq.bean.User;importcn.qlq.service.UserService;
@RestController/**自动返回的是json格式数据***/
public classUserController {
@AutowiredprivateUserService userService;
@RequestMapping("list")public Listlist(){
List list =userService.findAllUser();returnlist;
}
}
注意:@RestController 注解 (自动返回的是json格式数据)
(5)测试类
MySpringBootApplication.java
packagecn.qlq;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;
@SpringBootApplicationpublic classMySpringBootApplication {public static voidmain(String[] args) {//入口运行类
SpringApplication.run(MySpringBootApplication.class, args);
}
}
4.启动测试
---------------方法二:使用mybatis-spring整合的方式,也就是我们传统的方式--------------
这里我们推荐使用第二种,因为这样我们可以很方便的控制Mybatis的各种配置。
首先,创建一个Mybatis的配置类:
代码:
packagecn.qlq.config;importjavax.sql.DataSource;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.io.Resource;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importorg.springframework.core.io.support.ResourcePatternResolver;
@Configurationpublic classMyBatisConfig {
@Bean
@ConditionalOnMissingBean//当容器里没有指定的Bean的情况下创建该对象
publicSqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean= newSqlSessionFactoryBean();//设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);//设置mybatis的主配置文件
ResourcePatternResolver resolver = newPathMatchingResourcePatternResolver();
Resource mybatisConfigXml= resolver.getResource("classpath:mybatis/SqlMapConfig.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);//设置别名包
sqlSessionFactoryBean.setTypeAliasesPackage("cn.qlq.bean");returnsqlSessionFactoryBean;
}
}
然后,创建Mapper接口的扫描类MapperScannerConfig:
packagecn.qlq.config;importorg.mybatis.spring.mapper.MapperScannerConfigurer;importorg.springframework.boot.autoconfigure.AutoConfigureAfter;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class) //保证在MyBatisConfig实例化之后再实例化该类
public classMapperScannerConfig {//mapper接口的扫描器
@BeanpublicMapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer= newMapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("cn.qlq.mapper");returnmapperScannerConfigurer;
}
}
创建一个spring配置类,扫描的包,与读取的资源文件,创建数据源:
packagecn.qlq.config;importjavax.sql.DataSource;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;importcom.jolbox.bonecp.BoneCPDataSource;
@Configuration//通过该注解来表明该类是一个Spring的配置,相当于一个xml文件
@ComponentScan(basePackages = "cn.qlq") //配置扫描包
@PropertySource(value = { "classpath:db.properties"}, ignoreResourceNotFound = true)public classSpringConfig {
@Value("${jdbc.url}")privateString jdbcUrl;
@Value("${jdbc.driverClassName}")privateString jdbcDriverClassName;
@Value("${jdbc.username}")privateString jdbcUsername;
@Value("${jdbc.password}")privateString jdbcPassword;
@Bean(destroyMethod= "close")publicDataSource dataSource() {
BoneCPDataSource boneCPDataSource= newBoneCPDataSource();//数据库驱动
boneCPDataSource.setDriverClass(jdbcDriverClassName);//相应驱动的jdbcUrl
boneCPDataSource.setJdbcUrl(jdbcUrl);//数据库的用户名
boneCPDataSource.setUsername(jdbcUsername);//数据库的密码
boneCPDataSource.setPassword(jdbcPassword);//检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);//连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
boneCPDataSource.setIdleMaxAgeInMinutes(30);//每个分区最大的连接数
boneCPDataSource.setMaxConnectionsPerPartition(100);//每个分区最小的连接数
boneCPDataSource.setMinConnectionsPerPartition(5);returnboneCPDataSource;
}
}
application.properties:
server.port=80
logging.level.org.springframework=DEBUG
#springboot mybatis
#jiazai mybatis peizhiwenjian
#mybatis.mapper-locations = classpath:mapper/*Mapper.xml
#mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
#mybatis.type-aliases-package = cn.qlq.bean
#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
pom.xml:
4.0.0
cn.qlq
springboot-ssm
0.0.1-SNAPSHOT
war
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
mysql
mysql-connector-java
5.1.6
org.springframework
spring-tx
4.3.7.RELEASE
junit
junit
4.9
test
javax.servlet
servlet-api
2.5
provided
javax.servlet
jsp-api
2.0
provided
org.mybatis
mybatis
3.2.7
org.mybatis
mybatis-spring
1.2.2
com.jolbox
bonecp-spring
0.8.0.RELEASE
org.springframework
spring-jdbc
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.7
1.7
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.启动测试:
总结:我在使用过程中一开始是采用方式二搭建的环境,并且也是基于方式二整合的Mybatis的Pagehelper。但是在打成jar包运行的时候一直报一个错误,在MybatisConfig报错,所以最后我选择采用第一种方式搭建环境。现在上面的git地址也是基于第一种方式整合,但是提交记录有基于第二种的。git上面的项目也正在完善。目前已经整合; SpringTask、freemarker、thymeleaf等。。。。
补充:springboot项目热部署
1.第一种是基于springloaded
org.springframework.boot
spring-boot-maven-plugin
org.springframework
springloaded
1.2.6.RELEASE
这种方式不需要引入spring-boot-devtools相关依赖。
对dubug模式启动的 SpringBootApplication 有效,run模式启动的无效。对以maven方式运行的:spring-boot:run 也是有效的。
2.第二种是基于devtools
pom加入如下配置:
org.springframework.boot
spring-boot-devtools
application.properties里面增加如下配置:
#\u70ED\u52A0\u8F7D
spring.devtools.livereload.enabled=true
#\u70ED\u90E8\u7F72
spring.devtools.restart.enabled=false
对dubug模式启动的 SpringBootApplication 有效, run模式启动的无效,对以maven方式运行的:spring-boot:run 也是无效的。
这两种方式亲测有效。