個人筆記,如有錯誤,懇請批評指正。
新建項目后規劃好各層的包。

調整spring與mybatis配置文件
myBatis-config.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><!-- 通過別名簡化對類的使用 <typeAliases> <typeAlias type="cn.ustb.entity.Dept" alias="Dept" /></typeAliases> <mappers> <mapper resource="cn/ustb/entity/DeptMapper.xml" /> </mappers> --></configuration>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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 配置數據源,記得去掉myBatis-config.xml的數據源相關配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.MySQL.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="passWord" value="root" /> </bean> <!-- 配置session工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:myBatis-config.xml" /> </bean> <!-- 配置事務管理器,管理數據源事務處理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事務通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes><!-- 默認只處理運行時異常,可加rollback-for="Exception/Throwable"等處理所有異常或包括錯誤 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <!-- 配置切面織入的范圍,后邊要把事務邊界定在service層 --> <aop:config> <aop:advisor advice-ref="advice" pointcut="execution(* cn.ustb.scm.dao.impl.*.*(..))"/> </aop:config> <!-- 配置SessionTemplate,已封裝了繁瑣的數據操作--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <context:component-scan base-package="*"/></beans>如沒有建庫表,先建庫表,可參考如下sql:
drop database if exists mybatis;create database mybatis CHARACTER SET UTF8;use mybatis;create table dept( dept_id int primary key auto_increment, dept_name varchar(50), dept_address varchar(50));insert into dept(dept_name,dept_address) values('研發部一部','廣州');insert into dept(dept_name,dept_address) values('研發部二部','廣州');insert into dept(dept_name,dept_address) values('研發部三部','深圳');select * from dept;編寫實體類
public class Dept implements Serializable { private Integer deptId; private String deptName; private String deptAddress; ......}sql映射文件,并將相關信息映射到mybatis-config.xml文件。
<?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="cn.ustb.entity.DeptMapper"> <resultMap type="Dept" id="deptResultMap"> <id property="deptId" column="dept_id" /> <result property="deptName" column="dept_name" /> <result property="deptAddress" column="dept_address" /> </resultMap> <!-- id和命名空間用來定位SQL語句,parameterType表示參數的類型,resultMap返回類型 --> <select id="selectDept" parameterType="Integer" resultMap="deptResultMap"> <!--參數的寫法#{deptID} --> select * from dept where dept_id=#{deptID} </select> <insert id="insertDept" parameterType="Dept"> insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress}); </insert></mapper>myBatis-config.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><!-- 通過別名簡化對類的使用 --><typeAliases> <typeAlias type="cn.ustb.entity.Dept" alias="Dept" /></typeAliases> <mappers> <mapper resource="cn/ustb/entity/DeptMapper.xml" /> </mappers> </configuration>spring-mvc.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package="*"/></beans>web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>/jsp/main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html><head></head><body> this is main jsp</body></html>測試ssi整合
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html><head></head><body> <form action="dept/insert.action" method="post"> 名稱:<input type="text" name="deptName"><br> 地址:<input type="text" name="deptAddress"><br> <input type="submit" value="ok"> </form></body></html>中文亂碼處理,在web.xml中配置攔截器(參考前面)
<filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>接口包:cn.ustb.service 實現類包:cn.ustb.service.impl 編寫接口與實現類(實現類用@Service進行注解,dao接口結合下邊的配置,通過@Autowired方式注入代理實例),略。
略
添加如下內容:
<!-- 把事務邊界定在service層 --> <aop:config> <aop:advisor advice-ref="advice" pointcut="execution(* cn.ustb.scm.service.impl.*.*(..))"/> </aop:config><!-- 自動掃描組件,要把controller去除,他們是在spring-mvc.xml中配置,如果不去除會影響事務管理。 --> <context:component-scan base-package="cn.ustb"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 配置 轉換器,對于在basePackage設置的包(包括子包)下的接口類,如果在Mapper.xml文件中定義過, 將被轉換成spring的BEAN,在調用 的地方通過@Autowired方式將可以注入接口實例--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="basePackage" value="cn.ustb.scm.dao"/> </bean>spring-mvc.xml
<!-- 掃描所有的controller 但是不掃描service --> <context:component-scan base-package="cn.ustb"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>控制器類通過業務層接口調用業務層,業務層再通過dao接口(可刪除dao實現類,及測試類)獲取代理對象執行相關SQL,進行數據的操作
ED
新聞熱點
疑難解答