ssm和springboot整合
- 创建ssm+springboot项目
File–>New–>Project…
创建好的项目目录
注1:必须配置服务器和数据库相关信息
运行启动入口类的main方法
注:包和类不能乱改
-
配置服务器
server.port=8088
server.servlet.context-path=/s01 -
配置数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/zf?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123
#spring.jpa.database=mysql
#显示后台处理的SQL语句
#spring.jpa.show-sql=true -
#配置mybatis
#加载映射
mybatis.mapper-locations=classpath*:mapper/*.xml
#配置别名
mybatis.type-aliases-package=com.zking.zf.model
注1:使用maven运行mybatis-generator-maven-plugin插件生成Mapper.java和Mapper.xml和之前的配置一样复制即可
命令:mybatis-generator:generate -e
配置maven
- 修改XxxApplication扫描Mybatis的XxxMapper接口
@MapperScan(“com.zking.zf.mapper”)//将项目中对应的mapper类的路径加进来就可以了
public class ZfApplication{…}
6. spring Boot使用事务非常简单,首先使用注解@EnableTransactionManagement开启事务支持后,然后在访问数据库的Service方法上添加注解@Transactional便可
-
使用注解@EnableTransactionManagement开启事务支持
@EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的
<tx:annotation-driven />
…
public class ZfApplication {
…
} -
在Biz的类或方法上添加注解@Transactional
@Transactional //对所有方法有效
public interface IFwxxService{
}
public interface IFwxxService{
@Transactional //对指定方法有效
void add(Fwxx fwxx);
}
7 常见错误
- Cannot determine embedded database driver class for database type NONE
这是因为spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,DataSourceAutoConfiguration类使用了@Configuration注解向入spring
注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
解决方案两个:
-
在Application类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})阻止spring boot自动注入dataSource bean
2. 配置一个数据源即可 -
在JDBC使用的时候有时候会出现java.sql.SQLException:
The server time zone value '???ú±ê×??±??' is unrecognized or represents........
的错误,具体错误如下:
出现这个的原因是因为使用了较高版本的mysql驱动之后,它的返回的时间总是有问题,比实际时间要早8小时。
解决方案:在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B
public static final String URL=“jdbc:mysql://localhost:3306/jdbc01?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false”;
- 案例
1 配置辅助类
把application.properties
的后缀名改为yaml
application.yaml
server:
port: 8080
#server.servlet.context-path=/s01
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/t243?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password:
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.tang.ssm.model
logging:
level:
com.tang.ssm.mapper : debug
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="D:\\repository\\mvn-repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments