这里写自定义目录标题
1.依赖导入
<!--1.版本统一:-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<java.version>1.8</java.version>
<jdbc.driver.version>8.0.21</jdbc.driver.version>
<aspectj.version>1.7.4</aspectj.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<jsp-api.version>2.2</jsp-api.version>
<jstl.version>1.2</jstl.version>
<mybatis.version>3.2.5</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<slf4j.version>1.7.30</slf4j.version>
<logback.version>1.1.7</logback.version>
</properties>
<!--依赖导入-->
<dependencies>
<!--Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mybatis和Spring整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${jdbc.driver.version}</version>
</dependency>
</dependencies>
2.创建db.properties文件
druid.driverClassName=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://217.0.0.1/springstudy?useSSL=false&characterEnding=utf8&serverTimezone=Asia/Shanghai
druid.username=root
druid.password=123
druid.initialSize=10
druid.minIdle=6
druid.maxActive=50
druid.maxWait=60000
druid.timeBetweenEvictionRunsMillis=60000
druid.minEvictableIdleTimeMillis=300000
druid.validationQuery=SELECT 'x'
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false
druid.poolPreparedStatements=false
druid.maxPoolPreparedStatementPerConnectionSize=20
druid.filters=wall,stat
3.创建Spring配置文件,引入db.properties
<!--引入druid配置文件 db.properties文件在resources下-->
<context:property-placeholder location="classpath:db.properties" />
4.配置数据源
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--高版本的Driver可以自动识别数据库 而不再需要指定具体是哪一个Driver了-->
<property name="driverClassName" value="${druid.driverClassName}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<!-- 初始化连接数量 -->
<property name="initialSize" value="${druid.initialSize}" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${druid.minIdle}" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="${druid.maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}" />
<!--以下暂时可以不需要配置-->
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。 -->
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="${druid.filters}" />
</bean>
5.配置SqlSession工厂
<!--配置SqlSession工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
6.配置mapper扫描
<!--Mapper扫描配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--这里是你的扫描包 存放的是Mapper(DAO)接口-->
<property name="basePackage" value="dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
7.事务(以下二选一)
1.基于xml方式配置事务
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切面-->
<aop:config>
<aop:pointcut id="serviceOperation" expression="(execution(* service..*.*(..)))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>
2.基于注解配置事务
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--启用注解式事务 在类上的Transactional生效-->
<tx:annotation-driven transaction-manager="transactionManager"/>
在业务层添加@Transactional添加注解
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao dao;
@Transactional(readOnly = true)
@Override
public List<User> findAllUsers() {
return dao.findAllUsers();
}
@Override
public User findUserById(int id) {
return dao.findUserById(id);
}
@Override
public User findUserByUserName(String username) {
User user = dao.findUserByUserName(username);
System.out.println("dao查询到的user"+user);
return user;
}
}
8.测试
public class TestTranscation {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-*.xml");
UserService service = context.getBean("userService",UserService.class);
User user = new User();
user.setUsername("333");
user.setPassword("333");
Address address = new Address();
address.setAddress("四川省");
user.setAddress(address);
boolean result = service.addUser(user);
System.out.println(result);
}
}