Spring&Mybatis 整合開發基礎
阿新 • • 發佈:2020-12-31
首先新建MavenWeb專案,在webapp目錄下新建WEB-INF目錄,在其中新建web.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>SpringDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
< welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
匯入相關jar包:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wu.springdemo</groupId>
<artifactId>SpringDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- Mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<!-- Spring核心依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- Spring核心依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- Spring核心依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- 測試包 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
新建pojo,Student.java類:
package com.wu.pojo;
public class Student {
private String name;
private long id;
public Student() {}
public Student(String name,long id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
}
}
Spring物件建立的三種方式:
建立工廠類StudentFactory.java:
package com.wu.pojo;
public class StudentFactory {
public static Student getStudentStatic() {
return new Student();
}
public Student getStudent() {
return new Student();
}
}
在src下建立sprinp的配置檔案applicationContext.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 通過bean無參建立物件 -->
<!-- id表示物件標識 -->
<!-- class為需要建立的類的全路徑 -->
<!-- 預設為無參構造方法建立物件 -->
<bean id="student1" class = "com.wu.pojo.Student" />
<!-- 呼叫相應的有參構造方法建立物件 -->
<bean id = "student2" class = "com.wu.pojo.Student">
<!-- ref表示另一個bean value表示基本資料型別或者String型別 -->
<!-- 由name和id確定引數對應引數列表的有參構造方法 -->
<constructor-arg index = "0" value = "張三"></constructor-arg>
<constructor-arg index = "1" value = "2020" name = "id"></constructor-arg>
</bean>
<!-- 通過例項工廠建立物件 -->
<bean id = "factory" class = "com.wu.pojo.StudentFactory"></bean>
<bean id = "student3" factory-bean="factory" factory-method="getStudent" />
<!-- 通過靜態工廠建立物件 -->
<bean id = "student4" class = "com.wu.pojo.StudentFactory" factory-method="getStudentStatic" />
</beans>
建立測試類TestDemo.java:
package com.wu.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wu.pojo.Student;
public class TestDemo {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s1 = ac.getBean("student1",Student.class);
System.out.println("無參構造方法建立物件:"+s1);
Student s2 = ac.getBean("student2",Student.class);
System.out.println("有參構造方法建立物件:"+s2);
Student s3 = ac.getBean("student3",Student.class);
System.out.println("例項工廠建立物件:"+s3);
Student s4 = ac.getBean("student4",Student.class);
System.out.println("靜態工廠建立物件:"+s4);
// 獲取容器中管理的所有物件名
String[] names = ac.getBeanDefinitionNames();
for(String name : names) {
System.out.println(name);
}
}
}
結果:
屬性注入:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- -通過setter賦值 -->
<bean id = "student1" class="com.wu.pojo.Student">
<property name="name" value = "張三"></property>
<!-- 等效於以下語句 -->
<property name="id">
<value>2020</value>
</property>
<!-- 型別為set集合型別 -->
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</set>
</property>
<!-- 當list只要賦值一個時,只需<property name = "lists" value = "值">即可 -->
<property name="lists">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</property>
<property name="strs">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</array>
</property>
<property name="maps">
<map>
<entry key = "key1" value ="value1"></entry>
<entry key = "key2" value ="value2"></entry>
</map>
</property>
<!-- 屬性檔案 -->
<!-- <property name="propertys">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property> -->
</bean>
</beans>
Student.java:
package com.wu.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
private String name;
private long id;
private Set<?> sets;
private List<String> lists;
private String[] strs;
private Map<String,String> maps;
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public String[] getStrs() {
return strs;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public Set<?> getSets() {
return sets;
}
public void setSets(Set<?> sets) {
this.sets = sets;
}
public Student() {}
public Student(String name,long id) {
this.name = name;
this.id = id;
}
public Student(long id,String name) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", sets=" + sets + ", lists=" + lists + ", strs="
+ Arrays.toString(strs) + ", maps=" + maps + "]";
}
}
TestDemo.java:
package com.wu.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wu.pojo.Student;
public class TestDemo {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s1 = ac.getBean("student1",Student.class);
System.out.println("通過setter賦值:"+s1);
// 獲取容器中管理的所有物件名
String[] names = ac.getBeanDefinitionNames();
for(String name : names) {
System.out.println(name);
}
}
}
結果:
通過setter賦值:Student [name=張三, id=2020, sets=[1, 2, 3, 4], lists=[1, 2, 3, 4], strs=[1, 2, 3, 4], maps={key1=value1, key2=value2}]
student1
DI(依賴注入)
新建Book.java:
package com.wu.pojo;
public class Book {
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [name=" + name + ", author=" + author + "]";
}
}
修改Student.java:
package com.wu.pojo;
public class Student {
private String name;
private long id;
private Book book;
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", book=" + book + "]";
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
修改applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- -通過setter賦值 -->
<bean id = "student1" class="com.wu.pojo.Student">
<property name="name" value = "張三"></property>
<!-- 等效於以下語句 -->
<property name="id">
<value>2020</value>
</property>
<property name="book" ref = "book"></property>
</bean>
<bean id = "book" class = "com.wu.pojo.Book">
<property name="name">
<value>三體</value>
</property>
<property name="author">
<value>劉慈欣</value>
</property>
</bean>
</beans>
結果:
依賴注入:Student [name=張三, id=2020, book=Book [name=三體, author=劉慈欣]]
student1
book
Mybatis整合
在pom.xml檔案中新增包:
<!-- Mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<!-- Spring核心依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- Spring DAO依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- Spring DAO依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- Spring Web依賴包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- Spring 測試包 -->
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<!-- 日誌包 -->
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>