1,Spring
1.1 INTRODUCTION
**Official download address:** https://repo.spring.io/release/org/springframework/spring/
**Chinese documents:** https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference
jar packages to import:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--aop Woven bag--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!--Spring Packages required to operate the database--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--mybatis-Spring Integration package--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> </dependencies>
Prevent random coding
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
1.2 advantages
-
Spring is an open source and free framework (container)
-
Spring is a lightweight, non intrusive framework
-
Inversion of control (IOC), aspect oriented programming (AOP)
-
Support transaction processing and framework integration
Summary: Spring is a lightweight inversion of control (IOC) and aspect oriented programming (AOP) framework
1.3 composition
2. IOC object creation method
Nonparametric structure
<bean id="user" class="com.zhang.pojo.User"> <property name="name" value="zfstart"/>//set injection </bean>
Parametric structure
Constructed by subscript (index)
<bean id="user" class="com.zhang.pojo.User"> <constructor-arg index="0" value="bihao"/> </bean>
Constructed by type
<bean id="user" class="com.zhang.pojo.User"> <constructor-arg type="java.lang.String" value="bihao"/> </bean>
Construct by name
<bean id="user" class="com.zhang.pojo.User"> <constructor-arg name="name" value="bihao"/> </bean>
3. Spring configuration
1. Alias
Alias sets an alias for a bean. You can set multiple aliases
<alias name = "user" alias = "usernew"/>
2,import
Teamwork is achieved through import
<import resource="{path}/beans.xml"/>
4. Dependency injection (DI)
1. Constructor injection
Parametric structure injection
<bean id="user" class="com.zhang.pojo.User"> <constructor-arg name="name" value="bihao"/> </bean>
2. Set injection
pojo class: Student
private String name; private Address address; //Address object private String[ ]books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties info;
pojo class: Address
@Data @AllArgsConstructor @NoArgsConstructor public class Address { private String address; }
bean.xml file
<bean id="address" class="com.zhang.pojo.Address"> <property name="address" value="Jiaotong University"/> </bean> <bean id="student" class="com.zhang.pojo.Student"> <!--Normal value injection, value--> <property name="name" value="zfstart"/> <!--Bean Injection, ref--> <property name="address" ref="address"/> <!--array--> <property name="books"> <array> <value>The Dream of Red Mansion</value> <value>Journey to the West</value> <value>Water Margin</value> <value>Romance of the Three Kingdoms</value> </array> </property> <!--List--> <property name="hobbys"> <list> <value>listen to the music</value> <value>Knock code</value> <value>watch movie</value> </list> </property> <!--Map--> <property name="card"> <map> <entry key="ID" value="111111222222223333"/> <entry key="bank card" value="1321231312312313123"/> </map> </property> <!--set--> <property name="games"> <set> <value>LoL</value> <value>coc</value> <value>BOB</value> </set> </property> <!--nuLl--> <property name="wife"> <null/> </property> <!--Properties--> <property name="info"> <props> <prop key="Student number">123456</prop> <prop key="full name">zfstartt</prop> </props> </property> </bean>
3. p named and c named injection
User.java [there is no parameter construction method here]
public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
1. p namespace injection: a constraint file needs to be added to the header file
xmlns:p="http://www.springframework.org/schema/p"
<!--p Namespace injection,Attributes can be injected directly--> <bean id="user" class="com.zhang.pojo.User" p:name="zfstart" p:age="21"/>
2. c namespace injection: constraint files need to be added to the header file
xmlns:c="http://www.springframework.org/schema/c"
<!--c(Construction: Constructor)Namespace injection, through constructor injection, the attribute should still be set set method--> <bean id="user2" class="com.zhang.pojo.User" c:name="zt" c:age="21"/>
5. Automatic assembly of Bean
- Automatic assembly is a way for Spring to meet bean dependencies!
- Spring will automatically find in the context and automatically assemble properties for the bean!
There are three ways to assemble in Spring
- Configuration displayed in xml
- Display configuration in java
- Implicit automatic assembly bean [important]
1. Testing
Environment construction: one person has two pets
2. ByName auto assembly
<bean id="dog" class="com.zhang.pojo.Dog"/> <bean id="cat" class="com.zhang.pojo.Cat"/> <!-- byName:It will automatically find its own objects in the container context set Method beanid! --> <bean id="people" class="com.zhang.pojo.People" autowire="byName"> <property name="name" value="zfstart"/> </bean>
3. ByType auto assembly
<bean id="dog" class="com.zhang.pojo.Dog"/> <bean id="cat" class="com.zhang.pojo.Cat"/> <!--byType:It will automatically find the object with the same property type as its own object in the container context bean--> <bean id="people" class="com.zhang.pojo.People" autowire="byType"> <property name="name" value="zfstart"/> </bean>
Summary:
- When byname, you need to ensure that the IDs of all beans are unique, and the bean needs to be consistent with the value of the set method of the automatically injected attribute!
- When bytype, you need to ensure that the class of all beans is unique, and the bean needs to be consistent with the type of the automatically injected attribute!
4. Implementation using annotations
step
1. Import constraints: context constraints
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
2. Enable annotation configuration
<!--Enable annotation configuration--> <context:annotation-config/>
polular science:
@Nullable Field is marked with this annotation, indicating that this field can be null;
@Autowired
-
You can use it directly on the attribute! It can also be used in set mode!
-
Using Autowired, we don't need to write the Set method. The premise is that your auto assembled attribute exists in the IOC (Spring) container and conforms to the name byname!
Test code
public class People { //If required=false with Autowired defined is displayed, it indicates that this object can be null, otherwise it is not allowed to be null @Autowired(required = false)//You can remove the set method private Cat cat; @Autowired(required = false) private Dog dog; private String name; }
<bean id="dog" class="com.zhang.pojo.Dog"/> <bean id="cat" class="com.zhang.pojo.Cat"/> <bean id="people" class="com.zhang.pojo.People"/>
@Qualifier(value="xxx")
If the environment of @ Autowired auto assembly is complex and the auto assembly cannot be completed through an annotation [@ Autowired], we can use @ Qualifier(value = "xxx") to configure the use of @ Autowired and specify a unique bean object injection!
public class People { @Autowired @Qualifier(value = "cat1") //The two are used together private Cat cat; @Autowired @Qualifier(value = "dog2") private Dog dog; private String name; }
<bean id="dog1" class="com.zhang.pojo.Dog"/> <bean id="dog2" class="com.zhang.pojo.Dog"/> <bean id="cat1" class="com.zhang.pojo.Cat"/> <bean id="cat2" class="com.zhang.pojo.Cat"/> <bean id="people" class="com.zhang.pojo.People"/>
@Resource(name = "xxx")
public class People { @Resource(name = "cat1") private Cat cat; @Resource(name = "dog2") private Dog dog; private String name; }
Summary:
Difference between Resource and @ Autowired:
- They are used for automatic assembly and can be placed in the attribute field
- @Autowired is implemented by byType
- @Resource is implemented by byname by default. If the name cannot be found, it is implemented by byType. If both cannot be found, an error will be reported!
6. Using annotation development
1,bean
//Equivalent to < bean id = "user" class = "com. Zhang. POJO. User" / > @Component public class User { public String name; }
2. How are attributes injected
Two methods
//Equivalent to < bean id = "user" class = "com. Zhang. POJO. User" / > @Component public class User { //The value of the injected property is equivalent to < property name = "name" value = "zfstart" / > @Value("zfstart") public String name; }
@Component public class User { public String name; //The value of the injected property is equivalent to < property name = "name" value = "zfstart" / > @Value("zfstart") public void setName(String name) { this.name = name; } }
3. Derived annotation
@Component has several derived annotations. In web development, we will layer according to mvc three-tier architecture!
- pojo [@Component]
- dao [@Repository]
- service [@service]
- controller [@controller]
The four annotation functions are the same. They all represent registering a class in Spring and assembling beans
4. Automatic assembly
- @Autowired :Auto assembly pass type. name If Autowired If the attribute cannot be uniquely auto assembled, you need to pass the@Qualifier(value="xxx")- @Nullable Field is marked with this annotation, indicating that this field can be null; - @Resource :Automatic assembly by name. Type.
5. Scope
7. Configure Spring in java
Entity class
User.java
@Component public class User { private String name; public String getName() { return name; } @Value("zfstart") public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
configuration file
//This will also be registered in the container, because it is originally a @ Component //@Configuration means that this is a configuration class, just like the beans XML is the same @Configuration @ComponentScan("com.zhang") public class AppConfig { //Registering a bean is equivalent to a bean tag //The method name is id //The return value is the class attribute @Bean public User getUser(){ return new User(); } }
Test class
public class MyTest { public static void main(String[] args) { // AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); User getUser = context.getBean("getUser", User.class); System.out.println(getUser); } }
8. Agent mode
1. Static proxy
Static agent role analysis
- Abstract role: it is generally implemented using an interface or abstract class (UserDao)
- Real role: represented role (UserDaoImpl)
- Agent role: agent real role; After a real role is represented, some ancillary operations (UserDaoProxy) are generally performed
- Customer: use the agent role to perform some operations (Client)
code implementation
1. Abstract role
public interface UserDao { public void add(); }
2. Real role
public class UserDaoImpl implements UserDao{ @Override public void add() { System.out.println("Added a user"); } }
3. Agent role
public class UserDaoProxy implements UserDao{ private UserDaoImpl userDaoImpl; public UserDaoProxy() { } public void setUserDaoImpl(UserDaoImpl userDaoImpl) { this.userDaoImpl = userDaoImpl; } @Override public void add() { log("add");//Add log method userDaoImpl.add(); } //Log method public void log(String msg){ System.out.println("[DeBug]Used"+msg+"method"); } }
4. Client
public class Client{ public static void main(String[] args) { UserDaoImpl userDao = new UserDaoImpl(); UserDaoProxy userDaoProxy = new UserDaoProxy(); userDaoProxy.setUserDaoImpl(userDao); userDaoProxy.add(); } }
Benefits of static agents:
- It can make our real role more pure Stop paying attention to some public things
- Public business is done by agents The division of business is realized,
- When the public business expands, it becomes more centralized and convenient
Disadvantages:
- With more classes and more proxy classes, the workload becomes larger Reduced development efficiency
2. Dynamic agent
-
The role of dynamic agent is the same as that of static agent
-
The proxy class of dynamic proxy is generated dynamically The proxy class of static proxy is written in advance
-
Dynamic agents are divided into two categories: one is interface based dynamic agents, and the other is class based dynamic agents
-
- Dynamic agent based on interface -- JDK dynamic agent
- Class based dynamic proxy – cglib
- Now Java sist is used to generate dynamic proxy Baidu javasist
- We use the native code of JDK here. The rest is the same!
The dynamic proxy of JDK needs to know two classes
Core: InvocationHandler and Proxy,
[InvocationHandler: call handler]
Object invoke(Object proxy, method method, Object[] args); //parameter //Proxy - the proxy instance that calls the method //Method - the method corresponds to the instance that invokes the interface method on the proxy instance. The declared class of the method object will be the interface declared by the method, which can be the super interface of the proxy interface of the proxy class inheriting the method. //args - array of objects containing method calls that pass the parameter values of the proxy instance, or null if the interface method has no parameters. The parameters of the primitive type are contained in an instance of the appropriate primitive wrapper class, such as Java Lang. integer or Java lang.Boolean .
[Proxy: Proxy]
//Generate proxy class public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(),this); }
code implementation
1. Abstract role
public interface UserDao { public void add(); public void delete(); public void update(); public void query(); }
2. Real role
public class UserDaoImpl implements UserDao { @Override public void add() { System.out.println("Added a user"); } @Override public void delete() { System.out.println("Deleted a user"); } @Override public void update() { System.out.println("Modified a user"); } @Override public void query() { System.out.println("A user was queried"); } }
3. Agent role
//Use this class to automatically generate proxy classes public class ProxyInvocationHandler implements InvocationHandler { //Proxy interface private Object target; public void setTarget(Object target) { this.target = target; } //Generated proxy class public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } //Process the proxy instance and return the result @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log1(method.getName()); //The essence of dynamic agent is to use reflection mechanism Object result = method.invoke(target, args); log2(); return result; } public void log1(String msg){ System.out.println("Here is the beginning of the method, using"+msg+"method"); } public void log2(){ System.out.println("This is the end of the method"); } }
4. Client
public class Client { public static void main(String[] args) { //Real role UserDaoImpl userDao = new UserDaoImpl(); //delegable role ProxyInvocationHandler pih = new ProxyInvocationHandler(); //Set the objects that need to be represented pih.setTarget(userDao); //Dynamically generate proxy classes UserDao proxy = (UserDao) pih.getProxy(); proxy.update(); } }
Core: a dynamic agent generally represents a certain type of business. A dynamic agent can represent multiple classes, and the agent is the interface!
Benefits of dynamic agents
It has all the static agents. It also has all the static agents that don't have!
- It can make our real role more pure Stop paying attention to some public things
- Public business is done by agents The division of business is realized,
- When the public business expands, it becomes more centralized and convenient
- A dynamic agent, generally acting for a certain kind of business
- A dynamic proxy can proxy multiple classes, and the proxy is the interface!
9,AOP
What is AOP
The role of AOP in Spring
- Crosscutting concerns: methods or functions that span multiple modules of an application. That is, the part that has nothing to do with our business logic, but we need to focus on is crosscutting concerns. Such as log, security, cache, transaction and so on
- Aspect: a special object whose crosscutting concerns are modularized. That is, it is a class.
- Advice: work that must be completed in all aspects. That is, it is a method in a class.
- Target: the notified object.
- Proxy: an object created after notification is applied to the target object.
- Pointcut: the definition of the "place" where the aspect notification is executed.
- Join point: the execution point that matches the pointcut.
In spring AOP, crosscutting logic is defined through Advice. Spring supports five types of Advice:
Using Spring to implement AOP
[key] you need to import an aop weaving dependency package
<dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> </dependencies>
Method 1: Spring API interface
Advice notification
public class BeforeLog implements MethodBeforeAdvice { //Method: the method of the target object to execute //args: parameter //Target: target object @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"of"+method.getName()+"Method was executed"); } }
public class AfterLog implements AfterReturningAdvice { //Method: the method of the target object to execute //args: parameter //Target: target object //returnValue: return value @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("Yes"+method.getName()+"Method, the return result is:"+returnValue); } }
applicationContext.xml file
<bean id="userService" class="com.zhang.service.UserServiceImpl"/> <bean id="beforeLog" class="com.zhang.log.BeforeLog"/> <bean id="afterLog" class="com.zhang.log.AfterLog"/> <!--to configure aop:Import constraint header file--> <!--Method 1: use native Spring API Interface--> <aop:config> <!--Entry point: pointcut, expression: expression,execution(Location to execute)--> <aop:pointcut id="pointcut" expression="execution(* com.zhang.service.UserServiceImpl.*(..))"/> <!--Execute surround increase--> <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config>
Test class
@Test public void Test1(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //The dynamic proxy is an interface, so the return value type must be UserService UserService userService = context.getBean("userService", UserService.class); userService.add(); }
Mode 2: Section
notice
public class DiyPointCut { public void before(){ System.out.println("Before method execution!"); } public void after(){ System.out.println("After method execution!"); } }
applicationContext.xml
<!--Mode II--> <aop:config> <!--Custom section,ref Class to reference--> <aop:aspect ref="diy"> <!--breakthrough point--> <aop:pointcut id="pointcut2" expression="execution(* com.zhang.service.UserServiceImpl.*(..))"/> <!--notice--> <aop:before method="before" pointcut-ref="pointcut2"/> <aop:after method="after" pointcut-ref="pointcut2"/> </aop:aspect> </aop:config>
test
@Test public void Test2(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //The dynamic proxy is an interface, so the return value type must be UserService UserService userService = context.getBean("userService", UserService.class); userService.add(); }
Method 3: Annotation
@Component//Load class into IOC container @Aspect//Note that this is a cut plane public class AnnotationPointCut { @Before("execution(* com.zhang.service.UserServiceImpl.*(..))") public void before(){ System.out.println("Method 3: before method implementation"); } @After("execution(* com.zhang.service.UserServiceImpl.*(..))") public void after(){ System.out.println("Method 3: after method execution"); } //In surround enhancement, we can give a parameter to represent the point where we want to get the processing entry point; @Around("execution(* com.zhang.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint jp) throws Throwable { System.out.println("Surround front"); //Execution method Object proceed = jp.proceed(); System.out.println("After surround "); } }
applicationContext.xml
<!--Method 3: Annotation--> <!--Open annotation--> <context:component-scan base-package="com"/> <aop:aspectj-autoproxy/>
test
@Test public void Test1(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //The dynamic proxy is an interface, so the return value type must be UserService UserService userService = context.getBean("userService", UserService.class); userService.add(); }
10. Integrate Mybatis madness
Import related jar packages
- junit
- mybatis
- mysql database
- spring related
- aop weaving
- Mybatis spring integration package
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--aop Woven bag--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!--Spring Packages required to operate the database--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--mybatis-Spring Integration package--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> </dependencies>
Write configuration file
-
Write data source configuration * * [spring Dao. XML]**
<!--DataSource:use Spring Data source replacement mybatis Configuration of c3p0 dbcp druid We use it here Spring Provided JDBC:org.springframework.jdbc.datasource--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean>
-
sqlSessionFactory**[spring-dao.xml]**
<!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--binding mybatis configuration file--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--binding mapper of xml Files can also be found in mybatis Configuration in configuration file--> <property name="mapperLocations" value="classpath:com/zhang/mapper/UserMapper.xml"/> </bean>
-
sqlSessionTemplate**[spring-dao.xml]**
<!--SqlSessionTemplate:That's what we use sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--Only constructor injection can be used sqlSessionFactory,Because he didn't set method--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean>
-
An implementation class [UserMapperImpl.java] needs to be added to the interface
public class UserMapperImpl implements UserMapper{ //Originally, we used sqLSession to perform all our operations. Now we use sqLSessionTemplate; private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> findAll() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.findAll(); return userList; } }
-
Inject the implementation class written by yourself into Spring * * [applicationContext.xml]**
<import resource="spring-dao.xml"/> <bean id="userMapper" class="com.zhang.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean>
-
Test [MyTest]
public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> userList = userMapper.findAll(); for (User user : userList) { System.out.println(user); } } }
11. Integrate Mybatis power node
1. Catalogue
2. jar package
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!--Alibaba's database connection pool--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--aop Woven bag--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!--Spring Packages required to operate the database--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--mybatis-Spring Integration package--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies> <build> <!--maven Resource export problem--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
3. xml file under resource package
applicationContext.xml
<?xml version="1.0" encoding="utf8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userService" class="com.zhang.service.UserServiceImpl"> <property name="userMapper" ref="userMapper"/> </bean> <!--Import external files--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--Declare data source--> <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!--set Inject into DruidDataSource Provides information about connecting to the database--> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="${jdbc.maxActive}"/> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--set Inject, and pay the connection pool of the database to dataSource--> <property name="dataSource" ref="datasource"/> <!--mybatis Location of the master profile--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--establish mapper object MapperScannerConfigurer:Call internally gteMapper()Generate each Mapper Proxy object for interface--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--appoint sqlSessionFactory Object id--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--Specify package name,MapperScannerConfigurer All interfaces in this package will be scanned--> <property name="basePackage" value="com.zhang.mapper"/> </bean> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="utf8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--Logging--> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--Alias--> <typeAliases> <package name="com.zhang.pojo"/> </typeAliases> </configuration>
jdbc.properties
jdbc.url = jdbc:mysql://localhost:3306/mybatis?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai jdbc.username = root jdbc.password = root jdbc.maxActive = 20
4. mapper package
UserMapper.java
public interface UserMapper { int insertUser(User user); List<User> selectUsers(); }
UserMapper.xml
<?xml version="1.0" encoding="utf8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhang.mapper.UserMapper"> <insert id="insertUser" parameterType="User" > insert into user values (#{id},#{name}); </insert> <select id="selectUsers" resultType="User"> select * from user order by id desc; </select> </mapper>
5. pojo package
User.java
@Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; }
6. service package
UserService.java
public interface UserService { int addUser(User user); List<User> queryUsers(); }
UserServiceImpl.java
public class UserServiceImpl implements UserService { private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } @Override public int addUser(User user) { return userMapper.insertUser(user); } @Override public List<User> queryUsers() { return userMapper.selectUsers(); } }
12. Declarative transaction
Transaction ACID principle:
- Atomicity
- uniformity
- Isolation
- Multiple businesses may operate the same resource to prevent data corruption
- persistence
- Once the transaction is committed, no matter what happens to the system, the result will not be affected and will be written to the memory persistently
Transaction management in spring
-
Programming transactions: transactions need to be managed in code
-
Declarative transaction: AOP
applicationContext.xml file
<!--Configure declarative transaction manager--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--combination aop Implement transaction weaving--> <!--Configure transaction notifications--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--Configure transactions for those methods--> <!--To configure the propagation properties of a transaction: new propagation--> <tx:attributes> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="select" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--Configure transaction entry--> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* com.zhang.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config>
13. New feature - integrating junit
1. Import jar package
<!--Must be 4.12 Version above--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.6.RELEASE</version> </dependency>
2. Test class
import com.zhang.pojo.Book; import com.zhang.service.BookService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.ArrayList; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class MyTest { @Autowired private BookService bookService; //add to @Test public void add(){ Book book = new Book(); book.setUserId("1"); book.setUsername("java"); book.setUstatus("a"); bookService.addBook(book); } //modify @Test public void update(){ Book book = new Book(); book.setUserId("1"); book.setUsername("javaupdate"); book.setUstatus("b"); bookService.updateBook(book); } //delete @Test public void delete(){ bookService.deleteBook("1"); } //Query return value @Test public void selectCount(){ int count = bookService.findCount(); System.out.println(count); } //Query return object @Test public void selectOne(){ Book book= bookService.findOne("1"); System.out.println(book); } //Query return collection @Test public void selectList(){ List<Book> bookList= bookService.findAll(); for (Book book : bookList) { System.out.println(book); } } //Batch add @Test public void batchAdd(){ ArrayList<Object[]> batchArgs = new ArrayList<>(); Object[] o1={"2","java","b"}; Object[] o2={"3","java","c"}; Object[] o3={"4","java","d"}; batchArgs.add(o1); batchArgs.add(o2); batchArgs.add(o3); bookService.batchAdd(batchArgs); } @Test //Batch modification public void batchUpdate(){ ArrayList<Object[]> batchUpdate = new ArrayList<>(); Object[] o1 = {"c","f","2"}; Object[] o2 = {"c2","f","3"}; Object[] o3 = {"c3","f","4"}; batchUpdate.add(o1); batchUpdate.add(o2); batchUpdate.add(o3); bookService.batchUpdate(batchUpdate); } @Test //Batch delete public void batchDelete(){ ArrayList<Object[]> batchDelete = new ArrayList<>(); Object[] o1 = {"2"}; Object[] o2 = {"3"}; Object[] o3 = {"4"}; batchDelete.add(o1); batchDelete.add(o2); batchDelete.add(o3); bookService.batchDelete(batchDelete); } }