Spring学习
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency>
2023-06-09;
1.JMS Java message serice
2023-06-11;
2.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency>
3.
package com.kuang.user.dao; public interface UserDao { public String getUser(); }
4.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; public class UserDaoImpl implements UserDao { public String getUser() { return "默认查询用户数据"; } }
5.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; public class UserDaoMysqlImpl implements UserDao { @Override public String getUser() { return "MySQL获取用户数据"; } }
6.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; public class UserDaoOracleImpl implements UserDao { @Override public String getUser() { return "Oracle获取用户数据"; } }
7.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; public class UserDaoSqlserverImpl implements UserDao { @Override public String getUser() { return "SqlServer查用户数据"; } }
8.
package com.kuang.user.service; import com.kuang.user.dao.UserDao; public interface UserService { public String getUser(); public UserDao getUserDao(); public void setUserDao(UserDao userDao); }
9.
package com.kuang.user.service.impl; import com.kuang.user.dao.UserDao; import com.kuang.user.dao.impl.UserDaoImpl; import com.kuang.user.dao.impl.UserDaoMysqlImpl; import com.kuang.user.dao.impl.UserDaoOracleImpl; import com.kuang.user.service.UserService; public class UserServiceImpl implements UserService { // private UserDao userDao = new UserDaoImpl(); // private UserDao userDao = new UserDaoMysqlImpl(); // private UserDao userDao = new UserDaoOracleImpl(); private UserDao userDao; @Override public UserDao getUserDao() { return userDao; } @Override public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public String getUser() { return userDao.getUser(); } }
10.
import com.kuang.user.dao.impl.UserDaoImpl; import com.kuang.user.dao.impl.UserDaoMysqlImpl; import com.kuang.user.dao.impl.UserDaoOracleImpl; import com.kuang.user.dao.impl.UserDaoSqlserverImpl; import com.kuang.user.service.UserService; import com.kuang.user.service.impl.UserServiceImpl; public class MyTest { public static void main(String[] args) { // UserService userService = new UserServiceImpl(); UserService userService = new UserServiceImpl(); // userService.setUserDao(new UserDaoMysqlImpl()); // userService.setUserDao(new UserDaoImpl()); userService.setUserDao(new UserDaoSqlserverImpl()); System.out.println(userService.getUser()); } }
2023-06-13;
1.
package com.kuang.pojo;public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
2.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.kuang.pojo.Hello"> <property name="str" value="Spring"/> </bean> </beans>
3.
import com.kuang.pojo.Hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString()); } }
4.
20230614;
1.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDaoImpl" class="com.kuang.user.dao.impl.UserDaoImpl"/> <bean id="mysqlImpl" class="com.kuang.user.dao.impl.UserDaoMysqlImpl"/> <bean id="oracleImpl" class="com.kuang.user.dao.impl.UserDaoOracleImpl"/> <bean id="sqlServerImpl" class="com.kuang.user.dao.impl.UserDaoSqlserverImpl"/> <bean id="userServiceImpl" class="com.kuang.user.service.impl.UserServiceImpl"> <!-- <property name="userDao" ref="userDaoImpl"/>--> <!-- <property name="userDao" ref="mysqlImpl"/>--> <!-- <property name="userDao" ref="oracleImpl"/>--> <property name="userDao" ref="sqlServerImpl"/> </bean> </beans>
2.
import com.kuang.user.dao.impl.UserDaoImpl; import com.kuang.user.dao.impl.UserDaoMysqlImpl; import com.kuang.user.dao.impl.UserDaoOracleImpl; import com.kuang.user.dao.impl.UserDaoSqlserverImpl; import com.kuang.user.service.UserService; import com.kuang.user.service.impl.UserServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { // UserService userService = new UserServiceImpl(); // UserService userService = new UserServiceImpl(); // userService.setUserDao(new UserDaoMysqlImpl()); // userService.setUserDao(new UserDaoImpl()); // userService.setUserDao(new UserDaoSqlserverImpl()); // System.out.println(userService.getUser()); ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); System.out.println(userServiceImpl.getUser()); } }
3.IOC inversion of controll
4.
package com.kuang.pojo; public class User { private String name; public User(String name) { this.name = name; } /*public User() { System.out.println("User默认无参构造!"); }*/ public String getName() { return name; } public void setName(String name) { this.name = name; } public void show() { System.out.println("name=" + name); } }
5.
package com.kuang.pojo; public class Dog { private String name; public Dog() { System.out.println("Dog被创建!"); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
6.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <property name="name" value="秦疆"/>--> <!-- </bean>--> <!-- 通过下标 --> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <constructor-arg index="0" value="狂神说Java"/>--> <!-- </bean>--> <!-- 通过类型 --> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <constructor-arg type="java.lang.String" value="qinjiang"/>--> <!-- </bean>--> <!-- 通过属性名 --> <bean id="user" class="com.kuang.pojo.User"> <constructor-arg name="name" value="秦疆"/> </bean> <bean id="dog" class="com.kuang.pojo.Dog"> </bean> </beans>
7.
import com.kuang.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user = (User) context.getBean("user"); User user2 = (User) context.getBean("user"); System.out.println("user和user2引用同一对象:" + (user == user2)); // user.show(); } }
20230615;
1.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <property name="name" value="秦疆"/>--> <!-- </bean>--> <!-- 通过下标 --> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <constructor-arg index="0" value="狂神说Java"/>--> <!-- </bean>--> <!-- 通过类型 --> <!--<bean id="user" class="com.kuang.pojo.User"> <constructor-arg type="java.lang.String" value="qinjiang"/> </bean>--> <!-- 通过属性名 --> <bean id="user" class="com.kuang.pojo.User"> <constructor-arg name="name" value="秦疆"/> </bean> <!--<bean id="dog" class="com.kuang.pojo.Dog"> </bean>--> <alias name="user" alias="anotherNameOfUser"/> <bean id="dog" class="com.kuang.pojo.Dog" name="thirdNameOfDog, d1 d2;d3"> <property name="name" value="中华田园犬"/> </bean> </beans>
2.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="beans.xml"/> <import resource="beans2.xml"/> <import resource="beans3.xml"/> </beans>
3.
import com.kuang.pojo.Dog; import com.kuang.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { // ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // User user = (User) context.getBean("user"); // User user = (User) context.getBean("anotherNameOfUser"); // Dog dog = (Dog) context.getBean("thirdNameOfDog"); Dog dog = (Dog) context.getBean("d4"); // User user2 = (User) context.getBean("user"); // System.out.println("user和user2引用同一对象:" + (user == user2)); dog.show(); } }
4.DI dependency injection
5.
package com.kuang.pojo; public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
6.
package com.kuang.pojo; import java.util.*; public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String, String> card; private Set<String> games; private String wife; private Properties info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbies=" + hobbies + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
7.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.kuang.pojo.Address"> <property name="address" value="烟台"/> </bean> <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="秦疆"/> <property name="address" ref="address"/> <property name="books"> <array> <value>红楼梦</value> <value>三国演义</value> <value>水浒传</value> <value>西游记</value> </array> </property> <property name="hobbies"> <list> <value>跑步</value> <value>看搞笑视频</value> <value>哑铃</value> </list> </property> <property name="card"> <map> <entry key="身份证" value="370612111122331234"/> <entry key="银行卡" value="1234123412341234"/> </map> </property> <property name="games"> <set> <value>LOL</value> <value>CS</value> <value>CF</value> </set> </property> <property name="wife"> <null></null> </property> <property name="info"> <props> <prop key="driver">mysql</prop> <prop key="url">192.168.100.11</prop> <prop key="username">test</prop> <prop key="password">123456</prop> </props> </property> </bean> </beans>
8.
import com.kuang.pojo.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); // System.out.println(student.getAddress()); System.out.println(student.toString()); } }
20230616;
1.
package com.kuang.pojo; public class User { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
2.
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.kuang.pojo.User" p:name="张三" p:age="28"></bean> </beans>
3.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
4.
package com.kuang.pojo; public class Dog { private String name; private String age; public Dog() { } public Dog(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
5.
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--<bean id="dog" class="com.kuang.pojo.Dog" p:name="小黑" p:age="2"></bean>--> <bean id="dog2" class="com.kuang.pojo.Dog" c:name="小黄" c:age="3"></bean> </beans>
6.
import com.kuang.pojo.Dog; import com.kuang.pojo.Student; import com.kuang.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); // System.out.println(student.getAddress()); System.out.println(student.toString()); } @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user = context.getBean("user", User.class); System.out.println(user.toString()); } @Test public void dogTest() { ApplicationContext context = new ClassPathXmlApplicationContext("dogBeans.xml"); Dog dog = context.getBean("dog2", Dog.class); System.out.println(dog.toString()); } }
7.prototype [ˈproʊtəˌtaɪp]
8.
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--<bean id="dog" class="com.kuang.pojo.Dog" p:name="小黑" p:age="2"></bean>--> <!-- <bean id="dog2" class="com.kuang.pojo.Dog" c:name="小黄" c:age="3" scope="singleton"></bean>--> <bean id="dog2" class="com.kuang.pojo.Dog" c:name="小黄" c:age="3" scope="prototype"></bean> </beans>
9.
import com.kuang.pojo.Dog; import com.kuang.pojo.Student; import com.kuang.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); // System.out.println(student.getAddress()); System.out.println(student.toString()); } @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user = context.getBean("user", User.class); System.out.println(user.toString()); } @Test public void dogTest() { ApplicationContext context = new ClassPathXmlApplicationContext("dogBeans.xml"); Dog dog = context.getBean("dog2", Dog.class); System.out.println(dog.toString()); } @Test public void singletonTest() { ApplicationContext context = new ClassPathXmlApplicationContext("dogBeans.xml"); Dog dog = context.getBean("dog2", Dog.class); Dog dog2 = context.getBean("dog2", Dog.class); System.out.println(dog.hashCode()); System.out.println(dog2.hashCode()); System.out.println("scope为singleton时,dog==dog2吗?" + (dog == dog2)); } @Test public void prototypeTest() { ApplicationContext context = new ClassPathXmlApplicationContext("dogBeans.xml"); Dog dog = context.getBean("dog2", Dog.class); Dog dog2 = context.getBean("dog2", Dog.class); System.out.println(dog.hashCode()); System.out.println(dog2.hashCode()); System.out.println("scope为prototype时,dog==dog2吗?" + (dog == dog2)); } }
10.
package com.kuang.pojo; public class Cat { public void shout() { System.out.println("miao"); } }
11.
package com.kuang.pojo; public class Dog { public void shout() { System.out.println("wang"); } }
12.
package com.kuang.pojo; public class Person { private Cat cat; private Dog dog; private String name; public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "cat=" + cat + ", dog=" + dog + ", name='" + name + '\'' + '}'; } }
13.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.kuang.pojo.Cat"/> <!-- <bean id="dog" class="com.kuang.pojo.Dog"/>--> <bean id="dog2" class="com.kuang.pojo.Dog"/> <!--<bean class="com.kuang.pojo.Cat"/> <bean class="com.kuang.pojo.Dog"/>--> <!--<bean id="person" class="com.kuang.pojo.Person"> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> <property name="name" value="小明"/> </bean>--> <!--<bean id="person" class="com.kuang.pojo.Person" autowire="byName"> <property name="name" value="小明"/> </bean>--> <bean id="person" class="com.kuang.pojo.Person" autowire="byType"> <property name="name" value="小米"/> </bean> </beans>
14.
import com.kuang.pojo.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testAutowiredByName() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testAutowiredByType() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } }
20230617;
1.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <!-- <bean id="cat" class="com.kuang.pojo.Cat"/>--> <!-- <bean id="dog" class="com.kuang.pojo.Dog"/>--> <bean id="dog2" class="com.kuang.pojo.Dog"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <!-- <bean id="dog" class="com.kuang.pojo.Dog"/>--> <bean id="person" class="com.kuang.pojo.Person"/> <!--<bean class="com.kuang.pojo.Cat"/> <bean class="com.kuang.pojo.Dog"/>--> <!--<bean id="person" class="com.kuang.pojo.Person"> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> <property name="name" value="小明"/> </bean>--> <!--<bean id="person" class="com.kuang.pojo.Person" autowire="byName"> <property name="name" value="小明"/> </bean>--> <!--<bean id="person" class="com.kuang.pojo.Person" autowire="byType"> <property name="name" value="小米"/> </bean>--> </beans>
2.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.Nullable; public class Person { @Autowired(required = false) private Cat cat; @Autowired private Dog dog; private String name; /*public Person(@Nullable String name) { this.name = name; }*/ public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "cat=" + cat + ", dog=" + dog + ", name='" + name + '\'' + '}'; } }
3.
import com.kuang.pojo.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testAutowiredByName() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testAutowiredByType() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testContextAnnotation1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); // person.getCat().shout(); person.getDog().shout(); } }
20230618;
1.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog2" class="com.kuang.pojo.Dog"/> <bean id="dog3" class="com.kuang.pojo.Dog"/> <bean id="person" class="com.kuang.pojo.Person"/> </beans>
2.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.lang.Nullable; public class Person { @Autowired(required = false) private Cat cat; @Autowired @Qualifier(value = "dog2") private Dog dog; private String name; /*public Person(@Nullable String name) { this.name = name; }*/ public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "cat=" + cat + ", dog=" + dog + ", name='" + name + '\'' + '}'; } }
3.
import com.kuang.pojo.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void testContextAnnotation1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } }
4.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component //@Scope(value = "singleton") @Scope(value = "prototype") public class Pig { // @Value(value = "佩奇") private String name; public String getName() { return name; } // @Value(value = "小猪佩奇") @Value("小猪佩奇佩奇") public void setName(String name) { this.name = name; } }
5.
package com.kuang.dao; public interface PigDao { public void shout(); }
6.
package com.kuang.dao.impl; import com.kuang.dao.PigDao; import org.springframework.stereotype.Repository; @Repository public class PigDaoImpl implements PigDao { @Override public void shout() { System.out.println("哄哄"); } }
7.
package com.kuang.service; public interface PigService { public void shout(); }
8.
package com.kuang.service.impl; import com.kuang.dao.PigDao; import com.kuang.dao.impl.PigDaoImpl; import com.kuang.service.PigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PigServiceImpl implements PigService { @Autowired PigDao pigDaoImpl; @Override public void shout() { pigDaoImpl.shout(); } }
9.
package com.kuang.controller; import com.kuang.service.PigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class PigController { @Autowired private PigService pigService; public void shout() { pigService.shout(); } }
10.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- <context:component-scan base-package="com.kuang.pojo"/>--> <context:component-scan base-package="com.kuang"/> <context:annotation-config/> </beans>
11.
import com.kuang.controller.PigController; import com.kuang.pojo.Pig; import com.kuang.service.impl.PigServiceImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void testSpringAnnotation() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Pig pig = context.getBean("pig", Pig.class); System.out.println(pig.getName()); PigController pigController = context.getBean("pigController", PigController.class); pigController.shout(); Pig pig1 = context.getBean("pig", Pig.class); System.out.println("pig==pig1吗" + (pig == pig1)); } }
12.Repository
13.Qualifier
14.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao { @Override public String getUser() { return "默认查询用户数据"; } }
15.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoMysqlImpl implements UserDao { @Override public String getUser() { return "MySQL获取用户数据"; } }
16.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoOracleImpl implements UserDao { @Override public String getUser() { return "Oracle获取用户数据"; } }
17.
package com.kuang.user.dao.impl; import com.kuang.user.dao.UserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoSqlserverImpl implements UserDao { @Override public String getUser() { return "SqlServer查用户数据"; } }
18.
package com.kuang.user.service.impl; import com.kuang.user.dao.UserDao; import com.kuang.user.dao.impl.UserDaoImpl; import com.kuang.user.dao.impl.UserDaoMysqlImpl; import com.kuang.user.dao.impl.UserDaoOracleImpl; import com.kuang.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService {// private UserDao userDao; // @Qualifier("userDaoMysqlImpl") @Qualifier("userDaoImpl") @Autowired UserDao userDao; @Override public UserDao getUserDao() { return userDao; } @Override public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public String getUser() { return userDao.getUser(); } }
19.
<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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.kuang.user"/> <context:annotation-config/> </beans>
20.
import com.kuang.user.dao.impl.UserDaoImpl; import com.kuang.user.dao.impl.UserDaoMysqlImpl; import com.kuang.user.dao.impl.UserDaoOracleImpl; import com.kuang.user.dao.impl.UserDaoSqlserverImpl; import com.kuang.user.service.UserService; import com.kuang.user.service.impl.UserServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); System.out.println(userServiceImpl.getUser()); } }
21.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Hello { @Value("小明") private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
22.
<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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="com.kuang.pojo"/> </beans>
23.
import com.kuang.pojo.Hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { // ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString()); } }
24.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Dog { private String name; public Dog() { System.out.println("Dog被创建!"); } public String getName() { return name; } @Value("大黄") public void setName(String name) { this.name = name; } public void show() { System.out.println("name=" + name); } }
25.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; @Component public class User { @Value("张三") private String name; public User(@Nullable String name) { this.name = name; } /*public User() { System.out.println("User默认无参构造!"); }*/ public String getName() { return name; } public void setName(String name) { this.name = name; } public void show() { System.out.println("name=" + name); } }
26.
import com.kuang.pojo.Dog; import com.kuang.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Dog dog = context.getBean("dog", Dog.class); User user = context.getBean("user", User.class); user.show(); dog.show(); } }
27.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="com.kuang.pojo"/> </beans>
20230619;
1.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Address { @Value("烟台牟平4") private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
2.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Dog { private String name; private String age; public Dog() { } public Dog(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } @Value("大黄4") public void setName(String name) { this.name = name; } public String getAge() { return age; } @Value("4") public void setAge(String age) { this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
3.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.*; @Component public class Student { @Value("李四") private String name; @Autowired Address address; private String[] books; private List<String> hobbies; private Map<String, String> card; private Set<String> games; private String wife; private Properties info; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbies=" + hobbies + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
4.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class User { @Value("王五") private String name; @Value("28") private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
5.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="com.kuang.pojo"/> </beans>
6.
import com.kuang.pojo.Address; import com.kuang.pojo.Dog; import com.kuang.pojo.Student; import com.kuang.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void springAnnotationTest() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Address address = context.getBean("address", Address.class); System.out.println(address.toString()); Dog dog = context.getBean("dog", Dog.class); System.out.println(dog.toString()); Student student = context.getBean("student", Student.class); System.out.println(student.toString()); User user = context.getBean("user", User.class); System.out.println(user.toString()); } }
7.
package com.kuang.pojo; import org.springframework.stereotype.Component; @Component public class Cat { public void shout() { System.out.println("miao"); } }
8.
package com.kuang.pojo; import org.springframework.stereotype.Component; @Component public class Dog { public void shout() { System.out.println("wang"); } }
9.
import com.kuang.pojo.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testAutowiredByName() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testAutowiredByType() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testContextAnnotation1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = context.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); } @Test public void testSpringAnnotation2() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = context.getBean("person", Person.class); System.out.println(person.toString()); } }
10.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="com.kuang.pojo"/> </beans>
11.
import com.kuang.pojo.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void testSpringAnnotation2() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = context.getBean("person", Person.class); System.out.println(person.toString()); } }
12.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class User { @Value("王五") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
13.
package com.kuang.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class User { @Value("王五") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
14.
package com.kuang.config; import com.kuang.pojo.Dog; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.kuang.pojo")
public class KuangConfig { @Bean public Dog getDog() { return new Dog(); } }
15.
package com.kuang.config; import com.kuang.pojo.User; import com.kuang.pojo.Dog; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @ComponentScan(basePackages = "com.kuang.pojo") @Import(KuangConfig.class) public class MyConfig { @Bean public User getUser() { return new User(); } }
16.
import com.kuang.pojo.Dog; import com.kuang.config.MyConfig; import com.kuang.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); User user = context.getBean("getUser", User.class); System.out.println(user.toString()); Dog dog = context.getBean("getDog", Dog.class); System.out.println(dog.toString()); } }
17.AOP aspect oriented programming
20230620;
1.
package com.kuang.demo01; public interface Rent { public void rent(); }
2.
package com.kuang.demo01; public class Landlord implements Rent { @Override public void rent() { System.out.println("房东要出租房子"); } }
3.
package com.kuang.demo01; public class Proxy implements Rent { private Landlord landlord; public Proxy() { } public Proxy(Landlord landlord) { this.landlord = landlord; } @Override public void rent() { showings(); landlord.rent(); signLeaseContract(); collectIntermediaryFees(); } public void showings() { System.out.println("中介带着看房"); } public void signLeaseContract() { System.out.println("签租赁合同"); } public void collectIntermediaryFees() { System.out.println("收中介费"); } }
4.
package com.kuang.demo01; public interface Rent { public void rent(); }
5.
package com.kuang.demo02; public interface UserService { public void add(); public void delete(); public void update(); public void query(); }
6.
package com.kuang.demo02; public class UserServiceImpl implements UserService { @Override public void add() { // System.out.println("[Debug]调用了add方法"); System.out.println("增加一个用户"); } @Override public void delete() { // System.out.println("[Debug]调用了delete方法"); System.out.println("删除一个用户"); } @Override public void update() { // System.out.println("[Debug]调用了update方法"); System.out.println("修改一个用户"); } @Override public void query() { // System.out.println("[Debug]调用了query方法"); System.out.println("查询一个用户"); } }
7.
package com.kuang.demo02; public class UserServiceProxy implements UserService { private UserServiceImpl userService; public void setUserService(UserServiceImpl userService) { this.userService = userService; } // public UserServiceProxy() { // // } // public UserServiceProxy(UserServiceImpl userService) { // this.userService = userService; // } @Override public void add() { log("add"); userService.add(); } @Override public void delete() { log("delete"); userService.delete(); } @Override public void update() { log("update"); userService.update(); } @Override public void query() { log("query"); userService.query(); } public void log(String msg) { System.out.println("[Debug](通过代理类横向编程)调用了" + msg + "方法"); } }
8.
package com.kuang.demo02; public class Client { public static void main(String[] args) { // UserServiceImpl userService = new UserServiceImpl(); // userService.add(); // UserServiceImpl userService = new UserServiceImpl(); // UserServiceProxy userServiceProxy = new UserServiceProxy(userService); // userServiceProxy.add(); UserServiceImpl userService = new UserServiceImpl(); UserServiceProxy userServiceProxy = new UserServiceProxy(); userServiceProxy.setUserService(userService); userServiceProxy.add(); } }
20230621;
1.invocation 调用;invoked 调用;
20230622;
2.
package com.kuang.demo04; public interface Rent { public void rent(); }
3.
package com.kuang.demo04; public class Landlord implements Rent { @Override public void rent() { System.out.println("房东出租房子"); } }
4.
package com.kuang.demo04; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyInvocationHandlerSingleFunctionVersion implements InvocationHandler { private Rent rent; public void setRent(Rent rent) { this.rent = rent; } public Object getProxy() { return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { showings(); Object result = method.invoke(rent, args); collectIntermediaryFees(); return result; } public void showings() { System.out.println("中介带着看房"); } public void collectIntermediaryFees() { System.out.println("收中介费"); } }
5.
package com.kuang.demo04; public class ClientSingleFunctionVersion { public static void main(String[] args) { Landlord landlord = new Landlord(); ProxyInvocationHandlerSingleFunctionVersion proxyInvocationHandlerSingleFunctionVersion = new ProxyInvocationHandlerSingleFunctionVersion(); proxyInvocationHandlerSingleFunctionVersion.setRent(landlord); Rent proxy = (Rent) proxyInvocationHandlerSingleFunctionVersion.getProxy(); proxy.rent(); } }
6.
package com.kuang.demo04; public interface UserService { public void add(); public void delete(); }
7.
package com.kuang.demo04; public class UserServiceFirstEditionImpl implements UserService { @Override public void add() { System.out.println("增加了一个用户(第一版)"); } @Override public void delete() { System.out.println("删除了一个用户(第一版)"); } }
8.
package com.kuang.demo04; public class UserServiceSecondEditionImpl implements UserService { @Override public void add() { System.out.println("增加了一个用户(第二版)"); } @Override public void delete() { System.out.println("删除了一个用户(第二版)"); } }
9.
package com.kuang.demo04; public class UserServiceThirdEditionImpl implements UserService { @Override public void add() { System.out.println("增加了一个用户(第三版)"); } @Override public void delete() { System.out.println("删除了一个用户(第三版)"); } }
10.
package com.kuang.demo04; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyInvocationHandler implements InvocationHandler { private Object target; public void setTarget(Object target) { this.target = target; } public Object getProxy() { return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { logBefore(method.getName()); Object result = method.invoke(target, args); logAfter(method.getName()); return result; } public void logBefore(String msg) { System.out.println("[Debug]" + msg + "方法执行前打印"); } public void logAfter(String msg) { System.out.println("[Debug]" + msg + "方法执行后打印"); } }
11.
package com.kuang.demo04; public class Client { public static void main(String[] args) { // UserServiceFirstEditionImpl userServiceFirstEdition = new UserServiceFirstEditionImpl(); // UserServiceSecondEditionImpl userServiceSecondEdition = new UserServiceSecondEditionImpl(); UserServiceThirdEditionImpl userServiceThirdEdition = new UserServiceThirdEditionImpl(); ProxyInvocationHandler proxyInvocationHandler = new ProxyInvocationHandler(); // proxyInvocationHandler.setTarget(userServiceFirstEdition); // proxyInvocationHandler.setTarget(userServiceSecondEdition); proxyInvocationHandler.setTarget(userServiceThirdEdition); UserService proxy = (UserService) proxyInvocationHandler.getProxy(); // proxy.add(); proxy.delete(); } }
20230623;
1.
package com.kuang.aopTest; public interface UserService { public void add(); public void delete(); }
2.
package com.kuang.aopTest; public class UserServiceImpl implements UserService { @Override public void add() { System.out.println("增加一个用户"); } @Override public void delete() { System.out.println("删除一个用户"); } }
3.
package com.kuang.aopTest; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class LogAfter implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("[Debug]执行了" + target.getClass().getName() + "类的" + method.getName() + "方法,返回值是" + returnValue); } }
4.
package com.kuang.aopTest; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class LogAfter implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("[Debug]执行了" + target.getClass().getName() + "类的" + method.getName() + "方法,返回值是" + returnValue); } }
5.
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userServiceImpl" class="com.kuang.aopTest.UserServiceImpl"/> <bean id="logBefore" class="com.kuang.aopTest.LogBefore"/> <bean id="logAfter" class="com.kuang.aopTest.LogAfter"/> <aop:config> <!-- <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.add())"/>--> <!-- <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.delete())"/>--> <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.*(..))"/> <aop:advisor pointcut-ref="pointcut" advice-ref="logBefore"/> <aop:advisor pointcut-ref="pointcut" advice-ref="logAfter"/> </aop:config> </beans>
6.
import com.kuang.aopTest.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean("userServiceImpl", UserService.class); userService.add(); userService.delete(); } }
7.
package com.kuang.aopDiyTest; public class DiyPoint { public void before() { System.out.println("[Debug]自定义切点 方法执行前执行"); } public void after() { System.out.println("[Debug]自定义切点 方法执行后执行"); } }
8.
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userServiceImpl" class="com.kuang.aopTest.UserServiceImpl"/> <!-- <bean id="logBefore" class="com.kuang.aopTest.LogBefore"/>--> <!-- <bean id="logAfter" class="com.kuang.aopTest.LogAfter"/>--> <!-- <aop:config>--> <!--<!– <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.add())"/>–>--> <!--<!– <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.delete())"/>–>--> <!-- <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.*(..))"/>--> <!-- <aop:advisor pointcut-ref="pointcut" advice-ref="logBefore"/>--> <!-- <aop:advisor pointcut-ref="pointcut" advice-ref="logAfter"/>--> <!-- </aop:config>--> <bean id="diyPoint" class="com.kuang.aopDiyTest.DiyPoint"/> <aop:config> <aop:aspect ref="diyPoint"> <aop:pointcut id="point" expression="execution(* com.kuang.aopTest.UserServiceImpl.*(..))"/> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config> </beans>
9.signature [ˈsɪɡnətʃə(r)]
10.
package com.kuang.aopByAnnotation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class AnnotationPointCut { @Before("execution(* com.kuang.aopTest.UserServiceImpl.*(..))") public void before() { System.out.println("[Debug]执行方法前执行@Before"); } @After("execution(* com.kuang.aopTest.UserServiceImpl.*(..))") public void after() { System.out.println("[Debug]执行方法后执行@After"); } @Around("execution(* com.kuang.aopTest.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("[Debug]环绕执行前@Around"); proceedingJoinPoint.proceed(); System.out.println("[Debug]环绕执行后@Around"); } }
11.
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userServiceImpl" class="com.kuang.aopTest.UserServiceImpl"/> <!-- <bean id="logBefore" class="com.kuang.aopTest.LogBefore"/>--> <!-- <bean id="logAfter" class="com.kuang.aopTest.LogAfter"/>--> <!-- <aop:config>--> <!--<!– <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.add())"/>–>--> <!--<!– <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.delete())"/>–>--> <!-- <aop:pointcut id="pointcut" expression="execution(* com.kuang.aopTest.UserServiceImpl.*(..))"/>--> <!-- <aop:advisor pointcut-ref="pointcut" advice-ref="logBefore"/>--> <!-- <aop:advisor pointcut-ref="pointcut" advice-ref="logAfter"/>--> <!-- </aop:config>--> <!-- <bean id="diyPoint" class="com.kuang.aopDiyTest.DiyPoint"/>--> <!-- <aop:config>--> <!-- <aop:aspect ref="diyPoint">--> <!-- <aop:pointcut id="point" expression="execution(* com.kuang.aopTest.UserServiceImpl.*(..))"/>--> <!-- <aop:before method="before" pointcut-ref="point"/>--> <!-- <aop:after method="after" pointcut-ref="point"/>--> <!-- </aop:aspect>--> <!-- </aop:config>--> <bean id="annotationPointCut" class="com.kuang.aopByAnnotation.AnnotationPointCut"/> <aop:aspectj-autoproxy/> </beans>
20230624;
1.lombok ['lɔmbɔk]
2.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-study</artifactId> <groupId>org.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-10-mybatis</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.26</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.0.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
3.
driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username = root password = root
4.
package com.kuang.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
5.
package com.kuang.mapper; import com.kuang.pojo.User; import java.util.List; public interface UserMapper { public List<User> selectUser(); }
6.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.mapper.UserMapper"> <!-- <select id="selectUser" resultType="com.kuang.pojo.User">--> <!-- select * from mybatis.user;--> <!-- </select>--> <select id="selectUser" resultType="user"> select * from mybatis.user; </select> </mapper>
7.
import com.kuang.mapper.UserMapper; import com.kuang.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class MyTest { @Test public void testBasicMyBatis() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user); } } }
20230625;
1.
<?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> <!--引入外部配置文件 --> <!-- <properties resource="db.properties"/>--> <typeAliases> <typeAlias type="com.kuang.pojo.User" alias="user"/> </typeAliases> <!--<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments>--> <!--<mappers> <mapper class="com.kuang.mapper.UserMapper"/> </mappers>--> <!--<settings> <setting name="" value=""/> </settings>--> </configuration>
2.
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
3.
package com.kuang.mapper; import com.kuang.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperImpl implements UserMapper { private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<User> selectUser() { UserMapper userMapper = sqlSessionTemplate.getMapper(UserMapper.class); return userMapper.selectUser(); } }
4.
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> </beans>
5.
import com.kuang.mapper.UserMapper; import com.kuang.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; import java.io.InputStream; import java.util.List; public class MyTest { @Test public void testBasicMyBatis() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user); } } @Test public void testSpringIntergrationMyBatisMethodOne() { // ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user.toString()); } } }
20230626;
1.
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/> </bean> </beans>
2.
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="spring-dao-test-sql-session-dao-support.xml"/> <bean id="userMapperImplBySqlSessionDaoSupport" class="com.kuang.mapper.UserMapperImplBySqlSessionDaoSupport"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
3.
package com.kuang.mapper; import com.kuang.pojo.User; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class UserMapperImplBySqlSessionDaoSupport extends SqlSessionDaoSupport implements UserMapper { @Override public List<User> selectUser() { // SqlSession sqlSession = getSqlSession(); // UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // return userMapper.selectUser(); return getSqlSession().getMapper(UserMapper.class).selectUser(); } }
4.
import com.kuang.mapper.UserMapper; import com.kuang.mapper.UserMapperImplBySqlSessionDaoSupport; import com.kuang.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; import java.io.InputStream; import java.util.List; public class MyTest { @Test public void testBasicMyBatis() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user); } } @Test public void testSpringIntergrationMyBatisMethodOne() { // ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user.toString()); } } @Test public void testSpringIntergrationMybatisUsingSqlSesionDaoSupport() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextTestSqlSessionDaoSupport.xml"); UserMapperImplBySqlSessionDaoSupport userMapperImplBySqlSessionDaoSupport = context.getBean("userMapperImplBySqlSessionDaoSupport", UserMapperImplBySqlSessionDaoSupport.class); List<User> users = userMapperImplBySqlSessionDaoSupport.selectUser(); for (User user : users) { System.out.println(user.toString()); } } }
20230627;
1.propagation /ˌprɒp.əˈɡeɪ.ʃən/ 传播;增殖繁殖;
2.
<?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> <typeAliases> <typeAlias type="com.kuang.pojo.User" alias="user"/> </typeAliases> </configuration>
3.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/> </bean> </beans>
4.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
5.
package com.kuang.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
6.
package com.kuang.mapper; import com.kuang.pojo.User; import java.util.List; public interface UserMapper { public List<User> queryUser(); }
7.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.mapper.UserMapper"> <select id="queryUser" resultType="user"> select * from mybatis.user; </select> </mapper>
8.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-study</artifactId> <groupId>org.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-11-trasaction</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.26</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
9.
package com.kuang.mapper; import com.kuang.pojo.User; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> queryUser() { return getSqlSession().getMapper(UserMapper.class).queryUser(); } }
10.
import com.kuang.mapper.UserMapper; import com.kuang.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class MyTest { @Test public void testEstablishEnvironment() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.queryUser(); for (User user : users) { System.out.println(user); } } }
20230628;
1.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.mapper.UserMapper"> <select id="queryUser" resultType="user"> select * from mybatis.user; </select> <insert id="addUser" parameterType="user"> insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd}); </insert> <delete id="deleteUser" parameterType="int"> <!-- 故意写错用于测试 --> deletes from mybatis.user where id = #{id}; </delete> </mapper>
2.
package com.kuang.mapper; import com.kuang.pojo.User; import java.util.List; public interface UserMapper { public List<User> queryUser(); public int addUser(User user); public int deleteUser(int id); }
3.
package com.kuang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private String pwd; }
4.
package com.kuang.mapper; import com.kuang.pojo.User; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> queryUser() { User user = new User(5, "xiaoming", "23232"); UserMapper userMapper = getSqlSession().getMapper(UserMapper.class); userMapper.addUser(user); userMapper.deleteUser(5); return userMapper.queryUser(); } @Override public int addUser(User user) { return getSqlSession().getMapper(UserMapper.class).addUser(user); } @Override public int deleteUser(int id) { return getSqlSession().getMapper(UserMapper.class).deleteUser(id); } }
5.
import com.kuang.mapper.UserMapper; import com.kuang.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class MyTest { @Test public void testEstablishEnvironment() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.queryUser(); for (User user : users) { System.out.println(user); } } }
6.
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/> </bean> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="query" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* com.kuang.mapper.*.*(..))"/> <aop:advisor id="txAdvisor" advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> </beans>
20230629;
1.repository [rɪˈpɒzətri]
2.
public class UserDaoMysqlImpl implements UserDao { @Override public String getUser() { return "MySQL获取用户数据"; } }
3.
public class UserServiceImpl implements UserService { UserDao userDao; @Override public UserDao getUserDao() { return userDao; } @Override public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public String getUser() { return userDao.getUser(); } }
4.
@Configuration public class MyConfig { @Bean("userDaoMysqlImpl") public UserDao getUserDaoMysqlImpl() { return new UserDaoMysqlImpl(); } @Bean(name = "userServiceImpl", autowire = Autowire.BY_TYPE) public UserService getUserServiceImpl() { return new UserServiceImpl(); } }
5.
public class MyTest { @Test public void testAnnotation() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); UserService userService = applicationContext.getBean("userServiceImpl", UserService.class); System.out.println(userService.getUser()); } }
20230701;
1.integration /ˌɪn.tɪˈɡreɪ.ʃən/ 集成
2.concept /ˈkɒn.sept/ 概念
3.artifact artefact /ˈɑː.tə.fækt/ 工件 构件
4.assume 假定假设;
5.instantate 实例化;
6.
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.26</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
7.
public interface UserDao { public String getUser(); }
8.
@Repository public class UserDaoImpl implements UserDao { @Override public String getUser() { return "基础查询用户"; } }
9.
@Repository public class UserDaoMysqlImpl implements UserDao { @Override public String getUser() { return "MySQL获取用户"; } }
10.
public interface UserService { public String getUser(); }
11.
@Service public class UserServiceImpl implements UserService { @Autowired UserDaoImpl userDao; @Autowired UserDaoMysqlImpl userDaoMysql; @Override public String getUser() { // return userDao.getUser(); return userDaoMysql.getUser(); } }
12.
@Configuration public class MyConfig { @Bean public UserServiceImpl userService() { return new UserServiceImpl(); } @Bean public UserDaoImpl userDao() { return new UserDaoImpl(); } @Bean public UserDaoMysqlImpl userDaoMysql() { return new UserDaoMysqlImpl(); } }
13.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); UserService userService = applicationContext.getBean("userService", UserService.class); System.out.println(userService.getUser()); } }
20230702;
1.idea shift+enter在下方插入行;
2.
@Data @Component public class User { @Value("tom") private String name; }
3.
@Configuration public class MyConfig { @Bean public User user() { return new User(); } }
4.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); User user = applicationContext.getBean("user", User.class); System.out.println(user.toString()); } }
5.
@Component @Data public class Dog { public Dog() { System.out.println("Dog初始化"); } @Value("大黄") private String name; }
6.
@Configuration public class MyConfig { @Bean public Dog dog() { return new Dog(); } }
7.
public class MyTest { @Test public void testInitialize() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Dog dog = applicationContext.getBean("dog", Dog.class); System.out.println(dog); } }
8.
@Component @Data public class Address { @Value("烟台") private String address; }
9.
@Component @Data public class User { @Autowired Address address; @Value("小明") private String name; @Value("") private String[] books; private List<String> hobbies; private Map<String, String> cards; private Set<String> games; private String married; private Properties info; }
10.
@Configuration public class MyConfig { @Bean public Address address() { return new Address(); } @Bean public User user() { return new User(); } }
11.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); User user = applicationContext.getBean("user", User.class); System.out.println(user); } }
12.
@Component public class Cat { public void shout() { System.out.println("miao"); } }
13.
@Component public class Dog { public void shout() { System.out.println("wang"); } }
14.
@Component @Data public class Person { @Autowired Cat cat; @Autowired Dog dog; @Value("张三") private String name; }
15.
@Configuration public class MyConfig { @Bean public Cat cat() { return new Cat(); } @Bean public Dog dog() { return new Dog(); } @Bean public Person person() { return new Person(); } }
16.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Person person = applicationContext.getBean("person", Person.class); person.getCat().shout(); person.getDog().shout(); System.out.println(person); } }
20230703;
1.
@Component @Data public class Pig { @Value("大黄") private String name; }
2.
public interface PigDao { public void shout(); }
3.
@Repository public class PigDaoImpl implements PigDao { @Override public void shout() { System.out.println("哄哄"); } }
4.
public interface PigService { public void shout(); }
5.
@Service public class PigServiceImpl implements PigService { @Autowired PigDaoImpl pigDao; @Override public void shout() { pigDao.shout(); } }
6.
@Controller public class PigController { @Autowired PigServiceImpl pigService; public void shout() { pigService.shout(); } }
7.
@Configuration public class MyConfig { @Bean public PigDaoImpl pigDao() { return new PigDaoImpl(); } @Bean public PigServiceImpl pigService() { return new PigServiceImpl(); } @Bean public Pig pig() { return new Pig(); } @Bean public PigController pigController() { return new PigController(); } }
8.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); PigController pigController = applicationContext.getBean("pigController", PigController.class); pigController.shout(); } }
9.
@Component public class Dog { private String name; @Value("大黄") public void setName(String name) { this.name = name; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + '}'; } }
10.
@Configuration public class MyConfig { @Bean public Dog dog() { return new Dog(); } }
11.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Dog dog = applicationContext.getBean("dog", Dog.class); System.out.println(dog); } }
12.invoke invocation 调用
13.
public interface Rent { public void rent(); }
14.
@Component public class Landlord implements Rent { @Override public void rent() { System.out.println("房东出租房子"); } }
15.
@Component public class IntermediaryProxy implements Rent { @Autowired Landlord landlord; @Override public void rent() { seeHouse(); landlord.rent(); collectIntermediaryFees(); } public void seeHouse() { System.out.println("中介带看房子"); } public void collectIntermediaryFees() { System.out.println("收中介费"); } }
16.
public interface UserDao { public void add(); }
17.
@Repository public class UserDaoMysqlImpl implements UserDao { @Override public void add() { System.out.println("MySQL增加用户"); } }
18.
@Repository public class UserDaoOracleImpl implements UserDao { @Override public void add() { System.out.println("Oracle增加用户"); } }
19.
@Repository public class UserDaoProxyImpl implements UserDao { @Autowired UserDaoMysqlImpl userDaoMysql; @Autowired UserDaoOracleImpl userDaoOracle; @Override public void add() { logBefore("add"); // userDaoMysql.add(); userDaoOracle.add(); logAfter("add"); } public void logBefore(String msg) { System.out.println("[Debug]" + msg + "方法执行前"); } public void logAfter(String msg) { System.out.println("[Debug]" + msg + "方法执行后"); } }
20.
@Configuration public class MyConfig { @Bean public Landlord landlord() { return new Landlord(); } @Bean public IntermediaryProxy intermediary() { return new IntermediaryProxy(); } @Bean public UserDaoMysqlImpl userDaoMysql() { return new UserDaoMysqlImpl(); } @Bean public UserDaoOracleImpl userDaoOracle() { return new UserDaoOracleImpl(); } @Bean public UserDaoProxyImpl userDaoProxy() { return new UserDaoProxyImpl(); } }
21.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); IntermediaryProxy intermediaryProxy = applicationContext.getBean("intermediary", IntermediaryProxy.class); intermediaryProxy.rent(); UserDaoProxyImpl userDaoProxy = applicationContext.getBean("userDaoProxy", UserDaoProxyImpl.class); userDaoProxy.add(); }
20230704;
1.delegate /ˈdel.ɪ.ɡət/ 委托;
2.
@Configuration public class MyConfig { @Bean public Landlord landlord() { return new Landlord(); } @Bean public ProxyInvocationHandler proxyInvocationHandler() { return new ProxyInvocationHandler(); } }
3.
@Component public class ProxyInvocationHandler implements InvocationHandler { // private Object target; @Autowired Landlord landlord; public Object getProxy() { return Proxy.newProxyInstance(this.landlord.getClass().getClassLoader(), this.landlord.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { operateBefore(method.getName()); // Object result = method.invoke(target, args); Object result = method.invoke(landlord, args); operateAfter(method.getName()); return result; } public void operateBefore(String msg) { System.out.println("[Debug]" + msg + "方法执行之前"); } public void operateAfter(String msg) { System.out.println("[Debug]" + msg + "方法执行之后"); } }
4.
public class MyTest { @Test public void test() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); ProxyInvocationHandler proxyInvocationHandler = applicationContext.getBean("proxyInvocationHandler", ProxyInvocationHandler.class); Rent proxy = (Rent) proxyInvocationHandler.getProxy(); proxy.rent(); } }
5.
@Component public class UserDaoDynamicProxyInvocationHandler implements InvocationHandler { @Autowired UserDaoMysqlImpl userDaoMysql; public Object getProxy() { return Proxy.newProxyInstance(this.userDaoMysql.getClass().getClassLoader(), this.userDaoMysql.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { doBeforeInvoke(method.getName()); Object result = method.invoke(userDaoMysql, args); doAfterInvoke(method.getName()); return result; } public void doBeforeInvoke(String msg) { System.out.println("[Debug]" + msg + "方法执行之前"); } public void doAfterInvoke(String msg) { System.out.println("[Debug]" + msg + "方法执行之后"); } }
6.
@Configuration public class MyConfig { @Bean public UserDaoMysqlImpl userDaoMysql() { return new UserDaoMysqlImpl(); } @Bean public UserDaoDynamicProxyInvocationHandler userDaoDynamicProxyInvocationHandler() { return new UserDaoDynamicProxyInvocationHandler(); } }
7.
public class MyTest { @Test public void testUserDaoDynamicProxy() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); UserDaoDynamicProxyInvocationHandler userDaoDynamicProxyInvocationHandler = applicationContext.getBean("userDaoDynamicProxyInvocationHandler", UserDaoDynamicProxyInvocationHandler.class); UserDao proxy = (UserDao) userDaoDynamicProxyInvocationHandler.getProxy(); proxy.add(); } }
20230705;
1.aspect /ˈæs.pekt/ 方面、层面;
2.
<?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:aop="http://www.springframework.org/schema/aop" 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"> <bean id="userService" class="com.kuang.aop.impl.UserServiceImpl"/> <bean id="myPointCut" class="com.kuang.pointcut.MyPointCut"/> <aop:aspectj-autoproxy/> </beans>
3.
public interface UserService { public void add(); }
4.
public class UserServiceImpl implements UserService { @Override public void add() { System.out.println("增加一个用户"); } }
5.
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency>
6.
@Aspect public class MyPointCut { @Before("execution(* com.kuang.aop.impl.UserServiceImpl.*(..))") public void logBefore() { System.out.println("[Debug]方法执行前执行@Before"); } }
7.
public class MyTest { @Test public void testBeforeByXML() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = applicationContext.getBean("userService", UserService.class); userService.add(); } }
20230707;
1.
public interface UserService { public void add(); public void delete(); }
2.
public class UserServiceImpl implements UserService { @Override public void add() { System.out.println("增加一个用户"); } @Override public void delete() { System.out.println("删除一个用户"); } }
3.
@Aspect public class MyPointCut { @Before("execution(* com.kuang.aop.impl.UserServiceImpl.*(..))") public void logBefore() { System.out.println("[Debug]方法执行前执行@Before"); } @After("execution(* com.kuang.aop.impl.UserServiceImpl.*(..))") public void logAfter() { System.out.println("[Debug]方法执行后执行@After"); } @Around("execution(* com.kuang.aop.impl.UserServiceImpl.*(..))") public void logAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("[Debug]方法执行前执行@Around"); proceedingJoinPoint.proceed(); System.out.println("[Debug]方法执行后执行@Around"); } }
4.
public class MyTest { @Test public void testBeforeByXML() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = applicationContext.getBean("userService", UserService.class); userService.add(); userService.delete(); } }
20230708;
1.
@Data public class User { private String id; private String name; private String pwd; }
2.
public interface UserMapper { public List<User> getUser(); }
3.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.kuang2.pojo.User" alias="user"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/kuang2/mapper/UserMapper.xml"/> </mappers> </configuration>
4.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang2.mapper.UserMapper"> <select id="getUser" resultType="user"> select * from mybatis.user; </select> </mapper>
5.
public class MyTest { @Test public void testBasicMybatis() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.getUser(); for (User user : users) { System.out.println(user); } } }
20230710;
1.
public class UserMapperImpl implements UserMapper { private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<User> getUser() { UserMapper userMapper = sqlSessionTemplate.getMapper(UserMapper.class); List<User> users = userMapper.getUser(); return users; } }
2.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.kuang2.pojo.User" alias="user"/> </typeAliases> </configuration>
3.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config-with-spring-first.xml"/> <property name="mapperLocations" value="classpath:/com/kuang2/mapper/*.xml"/> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
4.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.kuang2.mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> </beans>
5.
@Test public void testSpringMyBatisFirst() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextSpringMybatisFirst.xml"); UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class); List<User> users = userMapper.getUser(); for (User user : users) { System.out.println(user); } }
6.
public class UserMapperImplBySqlSessionDaoSupport extends SqlSessionDaoSupport implements UserMapper { @Override public List<User> getUser() { return getSqlSession().getMapper(UserMapper.class).getUser(); } }
7.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config-with-spring-first.xml"/> <property name="mapperLocations" value="classpath:/com/kuang2/mapper/*.xml"/> </bean> </beans>
8.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao-by-sql-session-dao-support.xml"/> <bean id="userMapper" class="com.kuang2.mapper.UserMapperImplBySqlSessionDaoSupport"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
9.
@Test public void testSpringMyBatisUsingSqlSessionDaoSupport() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextTestSqlSessionDaoSupport.xml"); UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class); List<User> users = userMapper.getUser(); for (User user : users) { System.out.println(user); } }
20230711;
1.propagation /ˌprɒp.əˈɡeɪ.ʃən/ 传播 --》事务传播行为;
2.
@Data @AllArgsConstructor public class User { private int id; private String name; private String pwd; }
3.
public interface UserMapper { public List<User> queryUser(); public int addUser(User user); public int deleteUser(int id); }
4.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang3.mapper.UserMapper"> <select id="queryUser" resultType="user"> select * from mybatis.user; </select> <insert id="addUser" parameterType="user"> insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd}); </insert> <!-- 故意写错用于测试事务 --> <delete id="deleteUser" parameterType="java.lang.Integer"> deletes from mybatis.user where id = #{id}; </delete> </mapper>
5.
<?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> <typeAliases> <typeAlias type="com.kuang3.pojo.User" alias="user"/> </typeAliases> </configuration>
6.
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper { @Override public int addUser(User user) { return getSqlSession().getMapper(UserMapper.class).addUser(user); } @Override public int deleteUser(int id) { return getSqlSession().getMapper(UserMapper.class).deleteUser(id); } @Override public List<User> queryUser() { User user = new User(5, "pingFanLiLiang", "123123"); UserMapper userMapper = getSqlSession().getMapper(UserMapper.class); userMapper.addUser(user); userMapper.deleteUser(5); return userMapper.queryUser(); } }
7.
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config-for-transaction.xml"/> <property name="mapperLocations" value="classpath:/com/kuang3/mapper/*.xml"/> </bean> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <!-- <tx:method name="add" propagation="REQUIRED"/>--> <!-- <tx:method name="delete" propagation="REQUIRED"/>--> <!-- <tx:method name="update" propagation="REQUIRED"/>--> <!-- <tx:method name="query" read-only="true"/>--> <tx:method name="queryUser" propagation="REQUIRED"/> <!-- <tx:method name="*" propagation="REQUIRED"/>--> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointCut" expression="execution(* com.kuang3.mapper.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> </beans>
8.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao-for-transaction.xml"/> <bean id="userMapperImpl" class="com.kuang3.mapper.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
9.
public class MyTest { @Test public void testBaseSpringMyBatis() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextForTransaction.xml"); UserMapper userMapper = applicationContext.getBean("userMapperImpl", UserMapper.class); List<User> users = userMapper.queryUser(); for (User user : users) { System.out.println(user); } } }