SpringBoot学习(mybatis,MySQL配置)
目录
代码地址:https://gitee.com/liangzhuangchang/spring-boot.git(欢迎大家一起学习进步)
-
DataSourceConfiguration 数据库配置(mybatis、mySql)
DataSourceConfiguration.java
package com.imooc.demo.config.dao;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.beans.PropertyVetoException;
@Configuration
//配置mybatis mapper的扫描路径
@MapperScan("com.imooc.demo.dao")
public class DataSourceConfiguration {
@Value("${spring.datasource.jdbc.driver}")
private String jdbcUrlDriver;
@Value("${spring.datasource.jdbc.url}")
private String jdbcUrl;
@Value("${spring.datasource.jdbc.username}")
private String jdbcUsername;
@Value("${spring.datasource.jdbc.password}")
private String jdbcPassword;
@Bean(name = "dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcUrlDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUsername);
dataSource.setPassword(jdbcPassword);
// 配置c3p0连接池的私有属性
// 连接池最大线程数
dataSource.setMaxPoolSize(30);
// 连接池最小线程数
dataSource.setMinPoolSize(10);
// 关闭连接后不自动commit
dataSource.setAutoCommitOnClose(false);
// 连接超时时间
dataSource.setCheckoutTimeout(10000);
// 连接失败重试次数
dataSource.setAcquireRetryAttempts(2);
return dataSource;
}
}
-
application.properties配置
server.port=8888
server.servlet.context-path=/demo
#数据库配置
spring.datasource.jdbc.driver=com.mysql.cj.jdbc.Driver
spring.datasource.jdbc.url=jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai&&useUnicode=true&characterEncoding=utf-8
spring.datasource.jdbc.username=root
spring.datasource.jdbc.password=123456
#Mybatis配置
mybatis_config_file=mybatis-config.xml
mapper_path=mapper/*.xml
entity_package=com.imooc.demo.entity
#打印sql语句
logging.level.com.imooc.demo=DEBUG
-
mybatis的配置:
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>
<!--使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列标签替换列别名 默认:true-->
<setting name="useColumnLabel" value="true"/>
<!--开启驼峰命名转换:Table{create_time} -> Entity{createTime}-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
-
SessionFactoryConfiguration的配置:
SessionFactoryConfiguration.java
package com.imooc.demo.config.dao;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class SessionFactoryConfiguration {
// @Autowired
// private DataSource dataSource;
//mybatis-config.xml配置文件的路径
@Value("${mybatis_config_file}")
private String mybatisConfigFilePath;
//mybatis mapper文件所在路径
@Value("${mapper_path}")
private String mapperPath;
//实体类所在的package
@Value("${entity_package}")
private String entityPackage;
// 构造器注入依赖,变量方式注入应该尽量避免,使用set方式注入或者构造器注入,
// 这两种方式的选择就要看这个类是强制依赖的话就用构造器方式,选择依赖的话就用set方法注入。
// 以下依赖注入相当于
final
DataSource dataSource;
@Autowired
public SessionFactoryConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
return sqlSessionFactoryBean;
}
}
-
TransactionManagementConfiguration事务配置:
-
TransactionManagementConfiguration.java
package com.imooc.demo.config.service;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {
final
DataSource dataSource;
public TransactionManagementConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public TransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
-
一个简单的md5算法:
package com.imooc.demo.util;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
/**
* 使用md5的算法进行加密
*/
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);// // 16进制数字
// 如果生成数字未满32位,需要前面补0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
引用了md5加密的文章出处:https://blog.csdn.net/kingtracy8/article/details/79648810