函式模板和類模板
阿新 • • 發佈:2020-08-27
Spring
一、spring簡介
spring框架 簡介spring是一個開源的設計層框架,它的提出主要是用來解決業務邏輯層和其它層的解耦的作用。 它是將面向介面的程式設計思想貫穿整個系統。spring是2003年興起的一個輕量級Java開發框架。 spring是一個分層的javase/ee full-stack(一站式)輕量級開源框架。 spring框架作用: 在專案主要是用來管理所有的物件,之前建立物件new xxx 在使用spring之後就不需要我們手動建立物件,物件 的建立全部交由spring來進行處理。 spring核心思想: AOP:面向切面程式設計 可以通過預編譯的方式和執行期動態代理來實現不需要修改原始碼的情況下給程式統一新增新的功能的技術。 IOC:反轉控制|控制反轉 以後建立物件不需要我們手動,全部物件的建立被spring反轉了,由spring來替我們建立物件。 ---DI:依賴注入 IOC的實現需要DI來進行技術實現。
二、spring的使用
1、使用步驟
(1)再pom檔案中匯入相關依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.14.RELEASE</version> </dependency>
(2)在resources檔案中建立applicationContext.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> </beans>
(3)建立實體類
public class User {
。。。
}
(4)在applicationContext.xml中配置bean
<!--
name:自定義名稱,用於找到此bean
class:對應實體類
-->
<bean name="user1" class="com.ahd.a.User"></bean>
(5)編寫測試類
public class TestUser {
public static void main(String[] args) {
//通常情況下使用new來例項化物件
User user = new User();
System.out.println(user);
//通過spring來例項化物件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) ac.getBean("user1");
System.out.println(user1);
}
2、bean標籤
(1)基本屬性
id:給注入的物件起一個名字保正唯一性
name:給注入物件起一個變數名(見名知意)
class:物件的全限定名
scope:決定物件的生命週期
singleton:spring容器只建立一次物件
prototype:物件在使用的時候建立,建立完成之後物件的宣告週期不在由spring管理了,每次都會建立新物件
init-method 初始化的方法 實體類建立物件的時候會自動執行
destroy-method 銷燬的方法 當呼叫close()方法時會執行銷燬方法
(2)值注入方式
- set注入方式
<!-- set注入方式-->
<bean name="user" class="com.ahd.a.User">
<!--
name:實體類中的屬性名
value:具體賦值的內容
-->
<property name="id" value="111"></property>
<property name="name" value="張三"></property>
<property name="age" value="18"></property>
</bean>
- 構造方法注入
<bean name="user2" class="com.ahd.a.User">
<!-- index屬性:是實體類構造方法中屬性位置的索引,從0開始
name:實體類中屬性名
type:資料型別
-->
<constructor-arg index="0" name="id" value="222" type="java.lang.Integer"></constructor-arg>
<constructor-arg index="1" name="name" value="lisi" type="java.lang.String"></constructor-arg>
<constructor-arg index="2" name="age" value="20" type="java.lang.Integer"></constructor-arg>
</bean>
<!--
實體類(User)中的屬性有其他類(Customer)
可以先使用set方式為其他類賦值,再使用ref標籤引用
-->
<bean name="customer" class="com.ahd.a.Customer">
<property name="id" value="444"></property>
<property name="name" value="wanwu"></property>
</bean>
<bean name="user3" class="com.ahd.a.User">
<constructor-arg index="0" name="id" value="222" type="java.lang.Integer"></constructor-arg>
<constructor-arg index="1" name="name" value="lisi" type="java.lang.String"></constructor-arg>
<constructor-arg index="2" name="age" value="20" type="java.lang.Integer"></constructor-arg>
<constructor-arg index="3" ref="customer"></constructor-arg>
</bean>
- p名稱命名方式
<bean name="user" class="com.ahd.a.Customer" p:id="11" p:name="lisi"></bean>
- spel表示式注入
<bean name="user1" class="com.ahd.a.User">
<property name="id" value="111"></property>
<property name="name" value="張三"></property>
<property name="age" value="18"></property>
</bean>
<!--spel表示式方式注入-->
<bean name="user2" class="com.ahd.a.User">
<property name="id" value="#{user1.id}"></property>
<property name="name" value="#{user1.name}"></property>
<property name="age" value="#{user1.age}"></property>
</bean>
- 陣列注入方式
<!-- 複雜型別,陣列注入方式-->
<!-- 如果陣列中要存放的資料只有一個那麼還是採用set方式注入-->
<bean name="arrEntity" class="com.ahd.a.ArrEntity">
<property name="arrStr" value="lisi"></property>
</bean>
<!-- 存放多個數據-->
<bean name="arrEntity2" class="com.ahd.a.ArrEntity">
<property name="arrStr"><!--為哪個陣列賦值-->
<array><!--使用array標籤對多個屬性賦值-->
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
<value>ddd</value>
</array>
</property>
</bean>
- 陣列中儲存其他物件
<!-- 陣列中儲存其他物件-->
<bean name="customer1" class="com.ahd.a.Customer">
<property name="id" value="44"></property>
<property name="name" value="eee"></property>
</bean>
<bean name="customer2" class="com.ahd.a.Customer">
<property name="id" value="55"></property>
<property name="name" value="rrr"></property>
</bean>
<bean name="arrEntity3" class="com.ahd.a.ArrEntity">
<property name="cusArr">
<array><!--使用array標籤對多個屬性賦值-->
<!--bean指定物件實體類的物件名-->
<ref bean="customer1"></ref>
<ref bean="customer2"></ref>
</array>
</property>
</bean>
- list注入
<!-- 如果數list要存放的資料只有一個-->
<bean name="listEntity1" class="com.a.ListEntity">
<property name="list" value="張三"></property>
</bean>
<!-- 如果數list要存放的資料有多個-->
<bean name="listEntity2" class="com.a.ListEntity">
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
<value>list4</value>
<value>list5</value>
</list>
</property>
</bean>
<!-- 如果數list要存放的資料有多個且為其他物件 先注入 然後↓-->
<bean name="listEntity3" class="com.a.ListEntity">
<property name="customers">
<list>
<ref bean="cumtomer1"></ref>
<ref bean="cumtomer2"></ref>
<ref bean="cumtomer3"></ref>
</list>
</property>
</bean>
- map注入
<!--複雜型別 map注入-->
<!--
key-ref:key可以以引用其它物件
value-ref: value 以引用其它物件
-->
<bean name="map1" class="com.a.MapEntity">
<property name="map">
<map>
<entry key="key1" value="value1"></entry>
<entry key="key2" value="value2"></entry>
<entry key="key3" value="value3"></entry>
<entry key="key4" value="value4"></entry>
</map>
</property>
</bean>
3、註解
(1)註解掃描
<context:component-scan base-package="com.ahd"></context:component-scan>
(2)分層的註解
@Component (實體類一般用Component)
他可以標註java分層的三層架構 <bean name="userController" class=com.anno.controller.UserController">
都使用components會自動識別Controller Dao和Service 註解自動識別類名首字母小寫當name
@Controller 標註web層
@Service 標註service層
@Repository 標註dao層
@Scope bean 生命週期
singleton:只建立一次
prototype:建立多次
@PostConstruct 初始化註解
@PreDestroy 銷燬註解
@Value 屬性賦值
可以直接在屬性上加
@Value("111")
private String id;
也可以在set方法上加
@Value("112")
public void setId(String id) {
this.id = id;
}
物件注入
@AutoWired 自動裝配
@Autowired
private User user;
三、SpringMVC
1、簡介
(1)mvc:
model :模型
view :檢視
controller: 控制器
(2)springmvc是spring c層提供的一個框架它的核心是DispatcherServlet,它的主要作用就是將請求分發給不同的後端處理器。 主要是由處理器對映器、處理器介面卡、檢視解析器這三個主要處理器來進行共同完成c層的工作。
springmvc其實主要是將業務邏輯處理完的資料結果和要返回的響應檢視封裝成一個物件這個物件是ModelAndView,最終展示給使用者。
2、mvc配置
(1)匯入包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant-launcher -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>1.9.6</version>
</dependency>
<!-- 配置serlvet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
(2)配置web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
(3)配置springmvc.xml
<!--配置包掃描-->
<context:component-scan base-package="com.ahd"></context:component-scan>
<!-- 配置介面卡-->
<mvc:annotation-driven />
<!--配置檢視解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3、引數接收
//接收方式1:屬性接收 適用 3~5個屬性
@RequestMapping("/getParam1")
public ModelAndView getParam1(String username,String password,String sex) throws Exception{
System.out.println(username+" "+password+" "+sex);
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
//接收方式2:物件接收
@RequestMapping("/getParam2")
public String getParam2(User user) throws Exception{
System.out.println(user.getUsername()+" "+user.getPassword()+" "+user.getSex());
return "index";
}
//接收方式3:原生HttpServletRequest接收
@RequestMapping("/getParam3")
public String getParam3(HttpServletRequest request) throws Exception{
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
System.out.println(username+" "+password+" "+sex);
return "index";
}
4、請求轉發、重定向
//請求轉發:forward:請求路徑
return "forward:/index.do";
//重定向:redirect:重定向路徑
return "redirect:/index.do";
5、json格式資料
導包 jackson-annotaions
接收前臺傳過來的json格式資料使用@RequestBody
將後臺處理完的資料以json格式的資料返回 @ResponseBody
四、ssm整合
1、整合思路
ssm(spring springmvc mybatis)整合思路:
1、導包 (spring核心包+aop包+事務包+整合Junit包+web包+webmvc包+日誌包+mybatis+spring整合mybatis包)
2、配置web.xml (載入spring核心配置檔案+springmvc核心配置檔案+編碼過濾器)
3、建立spring核心配置檔案applicationContext.xml
載入資料來源
配置連線池
配置aop事務
掃描包
4、建立springmvc核心配置檔案springmvc.xml
掃描controller層
配置註解驅動
配置檢視解析器
配置放行靜態資源
5、測試啟動
2、整合步驟
(1)導包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant-launcher -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>1.9.6</version>
</dependency>
<!-- 配置serlvet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
(2)配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 字元編碼過濾器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置spring監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--在web.xml中配置核心控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--一般會設定成過濾所有請求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(3)配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 引入外部資源配置檔案 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置資料來源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:mybatis.xml"></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置mapper包掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ahd.mapper"></property>
</bean>
<!-- 配置事務 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 轉播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置切面 = 切點+通知 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.ahd.service.*.*(..))" />
</aop:config>
<!-- 配置service包掃描 -->
<context:component-scan base-package="com.ahd.service"></context:component-scan>
</beans>
(4)配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd ">
<!-- 開啟註解掃描 -->
<context:component-scan base-package="com.ahd.controller"></context:component-scan>
<!-- 配置註解驅動 -->
<mvc:annotation-driven />
<!-- 檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--放行靜態資源-->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
</beans>
(4)測試啟動
- controller層(UserController)
package com.ahd.controller;
import com.ahd.entity.User;
import com.ahd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@Autowired
private UserService userService;
/**跳轉到增加頁面*/
@RequestMapping(value = "/save")
public String save() throws Exception{
return "save";
}
/**跳轉到刪除頁面*/
@RequestMapping(value = "/delete")
public String delete() throws Exception{
return "delete";
}
/**跳轉到更新頁面*/
@RequestMapping(value = "/update")
public String update() throws Exception{
return "update";
}
/**增加使用者*/
@RequestMapping(value = "saveUser")
public String saveUser(User user)throws Exception{
Integer flag = userService.saveUser(user);
if(flag != null){
return "success";
}else {
return "save";
}
}
/**查詢所有使用者*/
@RequestMapping(value = "findUserAll")
public ModelAndView findUserAll() throws Exception{
ModelAndView mv = new ModelAndView();
mv.setViewName("find");
mv.addObject("list", userService.findUserAll());
return mv;
}
/**根據id刪除使用者使用者*/
@RequestMapping(value = "/deleteUser")
public String deleteUser(Integer id) throws Exception{
Integer flag = userService.deleteUser(id);
if(flag != null){
return "success";
}else {
return "delete";
}
}
/**更新使用者資訊*/
@RequestMapping(value = "/updateUser")
public String updateUser(User user)throws Exception{
Integer flag = userService.updateUser(user);
if(flag != null){
return "success";
}else {
return "update";
}
}
}
- entity實體類(User)
package com.ahd.entity;
public class User {
/**id*/
private String id;
/**使用者名稱*/
private String username;
/**密碼*/
private String password;
public User() {
}
public User(String id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
- mapper層(UserMapper)
package com.ahd.mapper;
import com.ahd.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
/**儲存使用者*/
Integer saveUser(User user);
/**查詢所有使用者資訊*/
List<User> findUserAll();
/**根據id刪除使用者*/
Integer deleteUser(Integer id);
/**修改使用者資訊*/
Integer updateUser(User user);
}
<?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.ahd.mapper.UserMapper">
<insert id="saveUser" parameterType="com.ahd.entity.User">
INSERT INTO `user`(username,password) VALUES(#{username},#{password})
</insert>
<select id="findUserAll" resultType="com.ahd.entity.User">
SELECT * FROM `user`
</select>
<delete id="deleteUser" parameterType="java.lang.Integer">
DELETE FROM `user` WHERE id = #{id}
</delete>
<update id="updateUser" parameterType="com.ahd.entity.User">
UPDATE `user` SET username = #{username},password = #{password} WHERE id = #{id}
</update>
</mapper>
- service層(User Service)
package com.ahd.service;
import com.ahd.entity.User;
import java.util.List;
public interface UserService {
/**儲存使用者*/
Integer saveUser(User user);
/**查詢所有使用者*/
List<User> findUserAll();
/**根據id刪除使用者*/
Integer deleteUser(Integer id);
/**修改使用者資訊*/
Integer updateUser(User user);
}
package com.ahd.service.impl;
import com.ahd.entity.User;
import com.ahd.mapper.UserMapper;
import com.ahd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Integer saveUser(User user) {
return userMapper.saveUser(user);
}
@Override
public List<User> findUserAll() {
return userMapper.findUserAll();
}
@Override
public Integer deleteUser(Integer id) {
return userMapper.deleteUser(id);
}
@Override
public Integer updateUser(User user) {
return userMapper.updateUser(user);
}
}