本案例使用thymeleaf、mybatis、阿里数据连接池与springboot配置使用。thymeleaf是一种模板语言,可 以动态或者静态显示文本内容。
1.项目结构
2、构建springboot项目
通过idea的new
project构建springboot项目,如果mvn比较慢,建议更改maven目录下的conf中的setting.xml,找到mirrors,在里面加入这段话
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
3、添加mybatis、mysql、阿里数据连接池、thymeleaf配置
pom.xml中插入
<!-- 阿里数据连接池druid -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- MySQL数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- thymeleaf视图模块 -->
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如果出现了thymeleaf的错误,请在pom文件的properties的那里加上这两行,小编在中遇到了问题,就是加了这两行,原因是因为,最新版本的thymeleaf不支持springboot合并的核心类,建议直接加上,因为小编用的是最新版本的thymeleaf的包
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
4.接下来写注解版的配置文件,也就是java文件中进行注解配置,不需要用到application.properties这种xml文件了
在java上建一个文件夹,名字随便取,我取的是application,因为这一看就知道是配置文件,里面文件名随意取,不过一般还是按照标准的来命名,这样我们一眼就能知道配置的是哪个类
建立数据连接池类 DataSourceConfiguration.java
package application;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DataSourceConfiguration {
/**
* mysql 连接Driver
*/
private static final String DRIVERCLASS = "org.gjt.mm.mysql.Driver";
/**
* mysql 地址
*/
private static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
/**
* mysql 用户名
*/
private static final String USER = "root";
/**
* mysql 密码
*/
private static final String PASSWORD = "123456";
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(DRIVERCLASS);
dataSource.setUrl(URL);
dataSource.setUsername(USER);
dataSource.setPassword(PASSWORD);
dataSource.setInitialSize(1);
dataSource.setMinIdle(1);
dataSource.setMaxActive(20);
dataSource.setTestOnBorrow(false);
dataSource.close();
return dataSource;
}
}
建立mybatis mapperXML的配置MyBatisConfig.java
package application;
import java.io.IOException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
@Configuration
// 加上这个注解,使得支持事务
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
@Autowired
private DataSource dataSource;
//mapper.xml路径
private static final String Mapepers_Xml = "SqlMapper/*.xml";
public PlatformTransactionManager annotationDrivenTransactionManager() {
// TODO Auto-generated method stub
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Resource[] mappers = resourceResolver.getResources(ResourcePatternResolver.CLASSPATH_URL_PREFIX + Mapepers_Xml);
bean.setMapperLocations(mappers);
try {
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
建立mybatis 接口配置类MyBatisMapperScannerConfig.java
package application;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
// 因为这个对象的扫描,需要在MyBatisConfig的后面注入,所以加上下面的注解
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// 获取之前注入的beanName为sqlSessionFactory的对象
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
// 指定xml配置文件的路径
mapperScannerConfigurer.setBasePackage("com.dao");
return mapperScannerConfigurer;
}
}
SpringBootApplication.java启动类
package com.trouble.springboot_m;
import application.DataSourceConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
@Configuration
@Component
//避免数据库连接出问题,建议加上,不然会出错
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication
//扫描类
@ComponentScan({"com","application"})
public class SpringbootMApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMApplication.class, args);
}
}
后面的controller、service、serviceImpl、dao、mapper里的xml层,和ssm是一致的,把上面这些配置都弄好后就可以实现全注解无配置的springboot+mybatis+Thymeleaf了
案列原项目下载
链接: 链接:https://pan.baidu.com/s/1RIaFNUP07xFqisSzm_mV6g
提取码:7izk
有帮助记得给小作者点个赞哈