Mybatis3+Spring4+SpringMVC4 框架整合環境搭建
首先在整合這個框架的時候,想想其一般的步驟是怎樣的,先有個步驟之後,不至於在後面的搞混了,這樣在整合的時候也比較清晰些。
1 建立一個Web專案。
2 匯入Mybatis3、Spring4、SpringMVC4、連線資料庫(我使用的資料庫是mysql)的jar包。
我所用的包:
spring-websocket-4.2.0.RELEASE.jar
3 建立Mybatis3、Spring4、SpringMVC4、連線資料庫的配置檔案。
4 配置web.xml
1 <?xml version="1.0" encoding="UTF-8"
5 datasource.properties 連線資料庫
1 jdbc.driver=com.mysql.jdbc.Driver2 jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-83 jdbc.username=root4 jdbc.password=root
6 mybatis.cfg.xml檔案
<?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> </configuration>
7 mvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 8 http://www.springframework.org/schema/mvc 9 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">1011 <!-- 啟動註解,註冊服務,如驗證框架、全域性型別轉換器-->12 <mvc:annotation-driven/>13 14 15 <!-- 啟動自動掃描 -->16 <context:component-scan base-package="com.cy.ssm">17 <!-- 制定掃包規則 ,只掃描使用@Controller註解的JAVA類 -->18 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>19 </context:component-scan>20 21 22 <!-- 配置檢視解析器 -->23 <!--24 prefix和suffix:查詢檢視頁面的字首和字尾(字首[邏輯檢視名]字尾),25 比如傳進來的邏輯檢視名為WEB-INF/jsp/hello,則該該jsp檢視頁面應該存放在“WEB-INF/jsp/hello.jsp”; -->26 <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">27 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>28 <property name="prefix" value="/"></property> <!-- 我這裡的檢視直接放在WebRoot下的 -->29 <property name="suffix" value=".jsp"></property>30 </bean>31 </beans>
8 applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 9 http://www.springframework.org/schema/tx10 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd11 http://www.springframework.org/schema/context12 http://www.springframework.org/schema/context/spring-context-4.2.xsd">13 14 15 <!-- 開啟自動掃包 -->16 <context:component-scan base-package="com.cy.ssm">17 <!--制定掃包規則,不掃描@Controller註解的JAVA類,其他的還是要掃描 -->18 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>19 </context:component-scan>20 21 <!-- 啟動AOP支援 -->22 <aop:aspectj-autoproxy/>23 24 <!-- 引入外部資料來源配置資訊 -->25 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">26 <property name="locations">27 <value>classpath:datasource.properties</value>28 </property>29 </bean>30 31 <!-- 配置資料來源 -->32 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">33 <property name="driverClassName" value="${jdbc.driver}"></property>34 <property name="url" value="${jdbc.url}"></property>35 <property name="username" value="${jdbc.username}"></property>36 <property name="password" value="${jdbc.password}"></property>37 </bean>38 39 40 <!-- 配置Session工廠 -->41 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">42 <property name="dataSource" ref="dataSource"></property>43 <!-- 載入mybatis.cfg.xml檔案 -->44 <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>45 <!-- 自動掃描需要定義類別名的包,將包內的JAVA類的類名作為類別名 -->46 <property name="typeAliasesPackage" value="com.cy.ssm.beans"></property>47 </bean>48 49 <!-- 自動掃描所有的Mapper介面與檔案 -->50 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">51 <property name="basePackage" value="com.cy.ssm.mapper"></property>52 </bean>53 54 <!-- 配置事務管理器 -->55 <bean id="txManager"56 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">57 <property name="dataSource" ref="dataSource"></property>58 </bean>59 60 <!-- 定義個通知,指定事務管理器 -->61 <tx:advice id="txAdvice" transaction-manager="txManager">62 <tx:attributes>63 <tx:method name="delete*" propagation="REQUIRED" read-only="false"64 rollback-for="java.lang.Exception" />65 <tx:method name="save*" propagation="REQUIRED" read-only="false"66 rollback-for="java.lang.Exception" />67 <tx:method name="insert*" propagation="REQUIRED" read-only="false"68 rollback-for="java.lang.Exception" />69 <tx:method name="update*" propagation="REQUIRED" read-only="false"70 rollback-for="java.lang.Exception" />71 <tx:method name="load*" propagation="SUPPORTS" read-only="true"/>72 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>73 <tx:method name="search*" propagation="SUPPORTS" read-only="true"/>74 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>75 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>76 </tx:attributes>77 </tx:advice>78 79 <aop:config>80 <!-- 配置一個切入點 -->81 <aop:pointcut id="serviceMethods" expression="execution(* com.cy.ssm.service.impl.*ServiceImpl.*(..))" />82 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />83 </aop:config>84 85 </beans>
9 配置檔案都差不多配置好了,接下來就寫個測試的。
UserBean.java
1 package com.cy.ssm.beans; 2 3 import java.io.Serializable; 4 5 public class UserBean implements Serializable { 6 7 8 private static final long serialVersionUID = -2682305557890221059L; 9 private Integer id;10 private String username;11 private String password;12 private Double account;13 public UserBean() {14 super();15 // TODO Auto-generated constructor stub16 }17 public UserBean(Integer id, String username, String password, Double account) {18 super();19 this.id = id;20 this.username = username;21 this.password = password;22 this.account = account;23 }24 public Integer getId() {25 return id;26 }27 public void setId(Integer id) {28 this.id = id;29 }30 31 public String getUsername() {32 return username;33 }34 public void setUsername(String username) {35 this.username = username;36 }37 public String getPassword() {38 return password;39 }40 public void setPassword(String password) {41 this.password = password;42 }43 public Double getAccount() {44 return account;45 }46 public void setAccount(Double account) {47 this.account = account;48 }49 @Override50 public String toString() {51 return "UserBean [account=" + account + ", id=" + id + ", password="52 + password + ", username=" + username + "]";53 }54 }
UserMapper.java
1 package com.cy.ssm.mapper; 2 3 import java.util.List; 4 import java.util.Map; 5 6 7 import org.apache.ibatis.annotations.Delete; 8 import org.apache.ibatis.annotations.Insert; 9 import org.apache.ibatis.annotations.Options; 10 import org.apache.ibatis.annotations.Param; 11 import org.apache.ibatis.annotations.Result; 12 import org.apache.ibatis.annotations.ResultMap; 13 import org.apache.ibatis.annotations.Results; 14 import org.apache.ibatis.annotations.Select; 15 import org.apache.ibatis.annotations.Update; 16 17 import com.cy.ssm.beans.UserBean; 18 19 20 public interface UserMapper { 21 22 23 /** 24 * 登入 25 * @param userName 26 * @param password 27 * @return 28 * @throws Exception 29 */ 30 @Select("select * from t_user where username=#{un} and password=#{pw}") 31 @Results({ 32 33 @Result(id=true,property="id",column="id",javaType=Integer.class), 34 @Result(property="username",column="username",javaType=String.class), 35 @Result(property="password",column="password",javaType=String.class), 36 @Result(property="account",column="account",javaType=Double.class) 37 }) 38 public UserBean login(@Param("un")String username,@Param("pw")String password); 39 /** 40 * 新增使用者 41 * @param user 42 * @return 43 * @throws Exception 44 */ 45 @Insert("insert into t_user value (null,user.username,user.password,user.account)") 46 @Options(useGeneratedKeys=true,keyProperty="user.id") 47 public int insertUser(@Param("user")UserBean user) throws Exception; 48 49 50 /** 51 * 修改使用者 52 * @param user 53 * @param id 54 * @return 55 * @throws Exception 56 */ 57 @Update(" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}") 58 public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception; 59 60 /** 61 * 刪除使用者 62 * @param id 63 * @return 64