SSM框架集成

SSM

主流的項目架構(gòu)的三大框架

?SpringMVC: Web層框架,spring的一個模塊

?Spring:容器框架

?MyBatis:持久層框架


準(zhǔn)備相關(guān)jar包

spring依賴包


?SpringMVC依賴包


Mybatis依賴包


Mybatis支持的日志包log4j?


Mybatis和Spring框架集成的橋梁包


數(shù)據(jù)庫驅(qū)動包和連接池


?Jstl標(biāo)簽庫依賴包


各種配置文件





Mapper層

public interface UserMapper {

??? int insert(User user);

??? User? selectByPrimaryKey(Integerid);

??? List? selectList();

??? int delteByPrimaryKey(Integer id);?

}



Mapperxml文件

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEmapper

? PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

? "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mappernamespace="cn.zj.ssm.mapper.UserMapper">

??? <insertid="insert"parameterType="cn.zj.ssm.pojo.User">

??????? insert into user? (name,password,age)values(#{name},#{password},#{age})

??? </insert>


??? <selectid="selectByPrimaryKey"parameterType="Integer"resultType="cn.zj.ssm.pojo.User">

??????? select * from user? where id = #{id}

??? </select>


??? <selectid="selectList"? resultType="cn.zj.ssm.pojo.User">

??????? select * from user

??? </select>


? ? <deleteid="delteByPrimaryKey"parameterType="int">

? ????? delete from user where id = #{id}

? ? </delete>

</mapper>



Service層?完成項目層與層之間spring對象的創(chuàng)建和依賴關(guān)系的維護


@Service

public class? UserServiceImpl implements UserService {


??? @Autowired

??? private UserMapper mapper;


??? public int insert(User user) {

??????? return mapper.insert(user);

??? }


? ??public User selectByPrimaryKey(Integer id) {

??????? System.out.println(mapper);

??????? return mapper.selectByPrimaryKey(id);

??? }


??? public List<User> selectList() {

??????? return mapper.selectList();

??? }


??? public int? delteByPrimaryKey(Integer id) {

??????? return mapper.delteByPrimaryKey(id);

??? }

}


測試代碼


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:spring.xml")

public class? UserServiceTest {

??? @Autowired

??? private UserService service;


??? @Test

??? public void testInsert() {

??????? Useruser = new User(null, "喬峰", "qiaofeng", 30);

??????? int row = service.insert(user);

??? }


??? @Test

??? public void? testSelectByPrimaryKey() {

??????? Useruser = service.selectByPrimaryKey(8);

??? }


??? @Test

??? public void? testSelectList() throws Exception {

??????? Listusers = service.selectList();

??????? for (User user : users) {

??????????? System.out.println(user);

??????? }

??? }


}



applicationContext配置文件的配置

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

??? xmlns:p="http://www.springframework.org/schema/p"

??? xmlns:context="http://www.springframework.org/schema/context"

??? 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.xsd

? http://www.springframework.org/schema/context

? http://www.springframework.org/schema/context/spring-context.xsd

? http://www.springframework.org/schema/aop

? http://www.springframework.org/schema/aop/spring-aop.xsd

? http://www.springframework.org/schema/tx

? ?http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 設(shè)置注解配置包掃描位置-->

<context:component-scanbase-package="cn.zj.mybatis"/>

</beans>



MyBatis 框架SqlSessionFactory對象的創(chuàng)建

UserMapper 代理對象使用 SqlSession 操作對象創(chuàng)建?

?SqlSessionFactory工廠對象創(chuàng)建SqlSession 對象

?SqlSessionFactory對象如何創(chuàng)建

? ?1,和Spring框架集成之前 MyBatis框架自己讀取配置文件中的相關(guān)配置去創(chuàng)建

? ?2,和Spring框架集成之后交給Spring容器來創(chuàng)建

? ? ? ? ? 在?mybatis和spring集成的類查閱橋梁包

? ? ? ? ?org.mybatis.spring.SqlSessionFactoryBean創(chuàng)建 SqlSessionFactory




配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

??? xmlns:p="http://www.springframework.org/schema/p"

??? xmlns:context="http://www.springframework.org/schema/context"

??? 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.xsd

??????? http://www.springframework.org/schema/context

???????

? http://www.springframework.org/schema/context/spring-context.xsd

???????

? http://www.springframework.org/schema/aop

???????

? http://www.springframework.org/schema/aop/spring-aop.xsd

???????

? http://www.springframework.org/schema/tx

???????

? http://www.springframework.org/schema/tx/spring-tx.xsd

??????? ">



??? <!-- 配置讀取? db.properties 數(shù)據(jù)庫配置文件 -->

??? <context:property-placeholderlocation="classpath:db.properties"/>


??? <!-- 配置數(shù)據(jù)源連接池? -->

??? <beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close">

??????? <propertyname="driverClassName"value="${jdbc.driverClassName}"/>

??????? <propertyname="url"value="${jdbc.url}"/>

??????? <propertyname="username"value="${jdbc.username}"/>

??????? <propertyname="password"value="${jdbc.password}"/>

??????? <propertyname="maxActive"value="${jdbc.maxActive}"/>

??? </bean>



??? <!--

??????? 配置MyBatis框架的? SqlSessionFactoryBean 類,創(chuàng)建


??????? ?SqlSessionFactory工廠對象

??? ?-->

??? <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

??????? <!-- 1.注入數(shù)據(jù)源 -->

??????? <propertyname="dataSource"ref="dataSource"/>



??????? <!-- 2.配置映射文件 -->

??????? <propertyname="mapperLocations">

??????????? <array>


??????????????? <!-- 可以使用通配符 * 讀取? 目錄下面所有的配置文件 -->

??????????????? <value>classpath:cn/zj/mybatis/mapper/*Mapper.xml</value>

??????????? </array>

??????? </property>


??????? <!-- 3. 配置別名使用包掃描? -->

??????? <propertyname="typeAliasesPackage"value="cn.zj.mybatis.pojo"/>


??????? <!-- 4.讀取mybat-config.xml配置文件,此配置文件可能還會配一些mybatis框架的

??????????? 其他個性化配置

??????????? 實際項目開發(fā)可能不用配置

??????? ?-->

??????? ?<propertyname="configLocation"value="classpath:mybatis-config.xml"/>

??? </bean>

</beans>

創(chuàng)建MyBatis的Mapper接口的代理對象

使用橋梁包org.mybatis.spring.mapper.MapperFactoryBean<T> 創(chuàng)建UserMapper代理對象



此種方式每一個Mapper接口需要單獨配置,如果Mapper過多,創(chuàng)建Mapper可能造成配置代碼過多

? ? ?<!-- 創(chuàng)建UserMapper代理對象-創(chuàng)建單個Mapper對象


??? ?使用橋梁包 org.mybatis.spring.mapper.MapperFactoryBean<T> 創(chuàng)建 UserMapper代理對象

??? ? -->


??? ? <beanid="userMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">


??? ? ???? <!-- 注入SqlSessionFacotry對象 -->

??? ? ???? <propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>


??? ? ???? <!-- 注入UserMapper接口類型:底層創(chuàng)建UserMapper的代理對象 -->

??? ? ???? <propertyname="mapperInterface"value="cn.zj.mybatis.mapper.UserMapper"/>


??? ? </bean>



使用包掃描創(chuàng)建MyBatis的Mapper接口的代理對象



<!-- 批量創(chuàng)建Mapper代理對象 ,使用包掃描創(chuàng)建Mapper代理對象

? 使用橋梁包


? org.mybatis.spring.mapper.MapperScannerConfigurer

? -->


? <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

? ????? <!-- 配置需要創(chuàng)建Mapper接口代理對象對應(yīng)的包 -->

? ????? <propertyname="basePackage"value="cn.zj.mybatis.mapper"/>


? ????? <!-- 配置SqlSessionFactoryBean 的名稱,不是引用 -->

? ????? <propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>


? </bean>


MyBatis的事務(wù)管理器的配置

一般開發(fā),事務(wù)的管理都會使用aop切入到業(yè)務(wù)層


??? ? <!-- 配置事務(wù)管理器 -->

??? ? <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

??? ? ? <!-- 注入數(shù)據(jù)源 -->

??? ? ? <propertyname="dataSource"ref="dataSource"/>

??? ? </bean>


??? ? <!--

? spring事務(wù)配置 -->

??? ? <tx:adviceid="txAdvice"transaction-manager="transactionManager">


??? ? ? <!-- 事務(wù)屬性配置 -->

??? ? ? <tx:attributes>

??? ? ????? <!-- DQL :查詢操作,配置只讀事務(wù)? -->

??? ? ????? <tx:methodname="get*"read-only="true"isolation="REPEATABLE_READ"? propagation="REQUIRED"/>

??? ? ????? <tx:methodname="select*"read-only="true"isolation="REPEATABLE_READ"? propagation="REQUIRED"/>

??? ? ????? <tx:methodname="find*"read-only="true"isolation="REPEATABLE_READ"? propagation="REQUIRED"/>

??? ? ????? <tx:methodname="query*"read-only="true"isolation="REPEATABLE_READ"? propagation="REQUIRED"/>


??? ? ????? <!-- 其他 SQL :非只讀事務(wù) -->

??? ? ????? <tx:methodname="*"read-only="false"isolation="REPEATABLE_READ"? propagation="REQUIRED"/>


??? ? ? </tx:attributes>


??? ? </tx:advice>


??? ? <!-- 配置AOP 切入事務(wù) -->


??? ? <aop:config>

??? ? ? <!-- 切入點 -->

??? ? ? <aop:pointcutexpression="execution(*

? cn.zj.mybatis.service..*.*(..))"id="pt"/>


??? ? ? <!-- 切面 -->

??? ? ? <aop:advisoradvice-ref="txAdvice"pointcut-ref="pt"/>

??? ? </aop:config>



SpringMVC的集成

在web.xml配置SpringMVC的前端控制器

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

? http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">


??? <!-- 配置字符編碼過濾器 -->

??? <filter>

?????? <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>

?????? <url-pattern>/*</url-pattern>

??? </filter-mapping>



??? <!-- 配置前端控制器 -->

??? <servlet>

?????? <servlet-name>MVC</servlet-name>

??? ??? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

?????? <init-param>

?????????? <param-name>contextConfigLocation</param-name>


?????????? <!-- 讀取配置文件

????????????? 問題:有多個spring相關(guān)配置文件如何讀取

????????????? 如 :spring.xml 和springmvc.xml


????????????? 解決方案:

????????????????? 方案一:直接使用 統(tǒng)配 * 可以讀取多個有相同前綴的文件

????????????????? classpath:spring*.xml

????????????????? 方案二:先讀取一個配置文件 如果 spring.xml

????????????????? 再在spring.xml文件中使用<import>標(biāo)簽導(dǎo)入 springmvc.xml文件


?????????? ?-->


?????????? <param-value>classpath:spring.xml</param-value>

?????? </init-param>


?????? <load-on-startup>1</load-on-startup>


??? </servlet>

??? <servlet-mapping>

?????? <servlet-name>MVC</servlet-name>

?????? <url-pattern>*.do</url-pattern>

??? </servlet-mapping>


</web-app>

[if !supportLists]2.1.1.??[endif]springmvc.xml配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

??? xmlns:p="http://www.springframework.org/schema/p"

??? xmlns:mvc="http://www.springframework.org/schema/mvc"

??? 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

? http://www.springframework.org/schema/mvc

??????? http://www.springframework.org/schema/mvc/spring-mvc.xsd

??????? ">

??????? <!-- 配置springmvc的注解驅(qū)動 -->

??????? <mvc:annotation-driven/>


</beans>



編寫控制器 UserController


@Controller

@RequestMapping("/user")

public class UserController

? {


??? @Autowired

??? private UserService service;


??? @RequestMapping("/list")

??? public String list(Model m) {


??????? //調(diào)用service查詢所有用戶方法

??????? Listusers = service.selectList();


??????? //共享數(shù)據(jù)

??????? m.addAttribute("users", users);


??????? return "/WEB-INF/view/user_list.jsp";

??? }



??? @RequestMapping("/delete")

??? public String delete(Integer id) {

??????? System.out.println(id);


??????? //調(diào)用service層的刪除方法

??????? service.delteByPrimaryKey(id);


??????? return "redirect:/user/list.do";

??? }


}



user_list.jsp 頁面

在jsp頁面使用jstl標(biāo)簽庫需要先在頁面引入jstl 標(biāo)簽庫

<%@pagelanguage="java"contentType="text/html;

? charset=UTF-8"

??? pageEncoding="UTF-8"%>

<!-- 引入jstl標(biāo)簽庫? -->

<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<!DOCTYPEhtml>

<html>

<head>

<metacharset="UTF-8">

<title>Insert title here</title>

</head>

<body>

??? <h3>用戶列表</h3>


??? <tableborder="1"style="width: 500px;" cellspacing="0">

??????? <tr>

??????????? <th>id</th>

??????????? <th>名稱</th>

??????????? <th>密碼</th>

??????????? <th>年齡</th>

??????????? <th>操作</th>

??????? </tr>



??????? <c:forEachitems="${users}"var="user">

??????????? <tr>

??????????????? <td>${user.id}</td>

??????????????? <td>${user.name}</td>

??????????????? <td>${user.password}</td>

??????????????? <td>${user.age}</td>

??????????????? <td>

??????????????????? <ahref="javascript:void(0);"onclick="deleteByPrimaryKey(${user.id})">刪除</a>?

??????????????????? <ahref="">修改</a>

??????????????? </td>

??????????? </tr>

??????? </c:forEach>

??? </table>


<scripttype="text/javascript">


??? function deleteByPrimaryKey(userId){

??????? if(confirm("親,您確定刪除此條數(shù)據(jù)么?")){

??????????? ????window.location.href="${pageContext.request.contextPath}/user/delete.do?id="+userId;

??????? }

??? }

</script>

</body>

</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容