mirror of
https://gitee.com/coder-xiaomo/java-note
synced 2025-09-06 20:01:39 +08:00
2022.01.20 1229
This commit is contained in:
402
张博凯的Java学习笔记.md
402
张博凯的Java学习笔记.md
@@ -1528,6 +1528,8 @@ password=111111
|
||||
|
||||
##### 导入PageHelper依赖
|
||||
|
||||
<span id="PageHelper依赖"></span>
|
||||
|
||||
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper/5.3.0
|
||||
|
||||
```xml
|
||||
@@ -1606,6 +1608,8 @@ https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper/5.3.0
|
||||
|
||||
##### log4j依赖
|
||||
|
||||
<span id="log4j依赖"></span>
|
||||
|
||||
https://mvnrepository.com/artifact/log4j/log4j/1.2.17
|
||||
|
||||
```xml
|
||||
@@ -1621,6 +1625,8 @@ https://mvnrepository.com/artifact/log4j/log4j/1.2.17
|
||||
|
||||
##### 导入log4j.propertites文件
|
||||
|
||||
<span id="导入log4j_propertites文件"></span>
|
||||
|
||||
[log4j.properties](张博凯的Java学习笔记.assets\file\log4j.properties)
|
||||
|
||||
src/main/resources/log4j.properties
|
||||
@@ -3523,12 +3529,408 @@ public class SpringUserTest {
|
||||
}
|
||||
```
|
||||
|
||||
> 通过@RunWith,声明当前测试类位于Spring容器管理
|
||||
>
|
||||
> 通过@ContextConfiguration声明加载配置文件
|
||||
|
||||
运行结果
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
#### TKMapper快速开发
|
||||
|
||||
> tk.mapper是mybatis第三方提供的一个插件。简化接口,不需要映射文件
|
||||
|
||||
##### 导入依赖
|
||||
|
||||
> 基于上一个项目(Spring整合MyBatis)
|
||||
|
||||
https://mvnrepository.com/artifact/tk.mybatis/mapper/4.1.5
|
||||
|
||||
```xml
|
||||
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
|
||||
<dependency>
|
||||
<groupId>tk.mybatis</groupId>
|
||||
<artifactId>mapper</artifactId>
|
||||
<version>4.1.5</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
##### 修改Spring的核心配置文件
|
||||
|
||||
**去掉以下部分**
|
||||
|
||||
```xml
|
||||
<!-- mapper文件的路径 -->
|
||||
<property name="mapperLocations" value="mappers/*Mapper.xml"></property>
|
||||
<!-- [可选] 取别名:类名首字母小写 -->
|
||||
<property name="typeAliasesPackage" value="org.example"></property>
|
||||
<!-- [可选] MyBatis配置文件 -->
|
||||
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"></property> -->
|
||||
```
|
||||
|
||||
**修改以下部分**
|
||||
|
||||
```xml
|
||||
<!-- 配置mapper -->
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
```
|
||||
|
||||
改为
|
||||
|
||||
```xml
|
||||
<!-- 配置mapper -->
|
||||
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
```
|
||||
|
||||

|
||||
|
||||
##### 删除映射文件
|
||||
|
||||
src/main/resources/mappers/
|
||||
|
||||
##### 实体类添加注解
|
||||
|
||||
src/main/java/org/example/entity/User.java
|
||||
|
||||
```java
|
||||
package org.example.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import tk.mybatis.mapper.annotation.KeySql;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Table(name = "user") // 对应数据库表名
|
||||
public class User {
|
||||
/* ***** 如果表中字段名和实体类中属性名一致,就可以直接测试 ***** */
|
||||
@Id // 主键
|
||||
@KeySql(useGeneratedKeys = true) // 自增
|
||||
private Integer user_id;
|
||||
private String user_name;
|
||||
private String user_pwd;
|
||||
private String user_realname;
|
||||
private Integer user_age;
|
||||
|
||||
/* 其实写成驼峰命名,经测试也可以 */
|
||||
@Id // 主键
|
||||
@KeySql(useGeneratedKeys = true) // 自增
|
||||
private Integer userId;
|
||||
private String userName;
|
||||
private String userPwd;
|
||||
private String userRealname;
|
||||
private Integer userAge;
|
||||
|
||||
/* ***** 如果表中字段名和实体类中属性名不一致,需要谈价@Column注解 ***** */
|
||||
@Id // 主键
|
||||
@KeySql(useGeneratedKeys = true) // 自增
|
||||
@Column(name = "user_id")
|
||||
private Integer id;
|
||||
@Column(name = "user_name")
|
||||
private String name;
|
||||
@Column(name = "user_pwd")
|
||||
private String pwd;
|
||||
@Column(name = "user_realname")
|
||||
private String realname;
|
||||
@Column(name = "user_age")
|
||||
private Integer age;
|
||||
}
|
||||
```
|
||||
|
||||
##### mapper接口改为extends,接口函数删掉
|
||||
|
||||
src/main/java/org/example/mapper/UserMapper.java
|
||||
|
||||
```java
|
||||
package org.example.mapper;
|
||||
|
||||
import org.example.entity.User;
|
||||
import tk.mybatis.mapper.common.Mapper;
|
||||
|
||||
public interface UserMapper extends Mapper<User> {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
###### 测试类
|
||||
|
||||
```java
|
||||
package org.example.test;
|
||||
|
||||
import org.example.entity.User;
|
||||
import org.example.mapper.UserMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:applicationContext.xml")
|
||||
public class SpringUserTest {
|
||||
// 需要调用接口中的方法进行测试
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void show() {
|
||||
List<User> users = userMapper.selectAll();
|
||||
for (User user : users) {
|
||||
System.out.println(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
运行结果
|
||||
|
||||
```bash
|
||||
User(user_id=1, user_name=小明, user_pwd=13579, user_realname=张小明, user_age=20)
|
||||
User(user_id=2, user_name=ad, user_pwd=asda, user_realname=dasd, user_age=48)
|
||||
|
||||
Process finished with exit code 0
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Spring整合MyBatis整合log4j
|
||||
|
||||
> 基于上一个项目(TKMapper快速开发)
|
||||
|
||||
##### 导入依赖
|
||||
|
||||
[log4j](#log4j依赖)、commons-logging、slf4j-log4j12
|
||||
|
||||
**导入commons-logging依赖**
|
||||
|
||||
https://mvnrepository.com/artifact/commons-logging/commons-logging/1.2
|
||||
|
||||
```xml
|
||||
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
**导入slf4j-log4j12依赖**
|
||||
|
||||
https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12/1.7.25
|
||||
|
||||
```xml
|
||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.25</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
##### 添加log4j配置文件
|
||||
|
||||
[导入log4j.propertites文件](#导入log4j_propertites文件)
|
||||
|
||||
进行测试,日志成功输出
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
#### Spring和MyBatis整合后pageHelper分页插件的使用
|
||||
|
||||
##### 导入依赖
|
||||
|
||||
[导入PageHelper依赖](#PageHelper依赖)
|
||||
|
||||
##### 在Spring核心配置文件中配置拦截器
|
||||
|
||||
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
|
||||
|
||||
src/main/resources/applicationContext.xml
|
||||
|
||||
```xml
|
||||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
||||
...
|
||||
<!-- pageHelper分页插件拦截器 -->
|
||||
<property name="plugins">
|
||||
<array>
|
||||
<bean class="com.github.pagehelper.PageInterceptor">
|
||||
<property name="properties">
|
||||
<!--使用下面的方式配置参数,一行配置一个 -->
|
||||
<value>
|
||||
params=value1
|
||||
</value>
|
||||
</property>
|
||||
</bean>
|
||||
</array>
|
||||
</property>
|
||||
</bean>
|
||||
```
|
||||
|
||||
##### 测试类
|
||||
|
||||
src/test/java/org/example/test/SpringUserTest.java
|
||||
|
||||
```java
|
||||
package org.example.test;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.example.entity.User;
|
||||
import org.example.mapper.UserMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:applicationContext.xml")
|
||||
public class SpringUserTest {
|
||||
// 需要调用接口中的方法进行测试
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void show() {
|
||||
// 设置当前页,页面容量
|
||||
PageHelper.startPage(1,3);
|
||||
List<User> users = userMapper.selectAll();
|
||||
PageInfo<User> userPageInfo = new PageInfo<>(users);
|
||||
System.out.println(userPageInfo);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
运行结果
|
||||
|
||||
```bash
|
||||
PageInfo{pageNum=1, pageSize=3, size=3, startRow=1, endRow=3, total=5, pages=2, list=Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=5, pages=2, reasonable=false, pageSizeZero=false}[User(id=1, name=小明, pwd=13579, realname=张小明, age=20), User(id=2, name=ad, pwd=asda, realname=dasd, age=48), User(id=3, name=vb, pwd=13579, realname=张小明, age=20)], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### AOP注解事务的使用
|
||||
|
||||
##### 导入依赖
|
||||
|
||||
spring-aspects
|
||||
|
||||
https://mvnrepository.com/artifact/org.springframework/spring-aspects/5.2.19.RELEASE
|
||||
|
||||
```xml
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
<version>5.2.19.RELEASE</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### 修改Spring的核心配置文件
|
||||
|
||||
修改文件头,添加事务管理器配置
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
...
|
||||
|
||||
<!-- 事务管理器 -->
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource"></property>
|
||||
</bean>
|
||||
|
||||
<!-- 声明使用注解配置 -->
|
||||
<context:annotation-config></context:annotation-config>
|
||||
|
||||
<!-- 扫描范围 -->
|
||||
<context:component-scan base-package="org.example"></context:component-scan>
|
||||
|
||||
<!-- 注解事务管理配置 -->
|
||||
<tx:annotation-driven></tx:annotation-driven>
|
||||
</beans>
|
||||
```
|
||||
|
||||
##### 应用
|
||||
|
||||
创建UserService
|
||||
|
||||
src/main/java/org/example/service/UserService.java
|
||||
|
||||
```java
|
||||
package org.example.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public void addUser() {
|
||||
System.out.println("增加");
|
||||
}
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public int updateUser() {
|
||||
return 1;
|
||||
}
|
||||
@Transactional(propagation = Propagation.SUPPORTS) // 查询不需要事务
|
||||
public int selectUser() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
>**Spring中七种Propagation类的事务属性详解**
|
||||
>
|
||||
> REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
|
||||
>
|
||||
> SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
|
||||
>
|
||||
> MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
|
||||
>
|
||||
> REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
|
||||
>
|
||||
> NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
|
||||
>
|
||||
> NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
|
||||
>
|
||||
> NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。
|
||||
|
||||
> REQUIRED和SUPPORTS用的较多。REQUIRED→增删改,SUPPORTS→查
|
||||
|
||||
|
||||
|
||||
### SpringMVC框架
|
||||
|
||||
> Web框架,Servlet角色
|
||||
|
Reference in New Issue
Block a user