快速入门SSM整合配置建立第一个SSM项目模板
場景
對于新手來說,學過Spring、SpringMVC、Mybatis,但是將其整合起來搭建項目就
比較困難。
環境
Eclipse+Mysql5.7+jdk8,沒有搭建Maven項目,入門已經將需要jar包整理好。
實現
新建項目
1.打開Eclipse--File--new--Dynamic Web Project
2.輸入ssmJarTemplate項目名,點擊Next--Next,勾選生成web.xml
?
項目建成后的目錄為
3.下載SSM整合所需的jar包,然后將里面的jar包復制到項目下的WEB-INF 下的lib下
SSM整合所需jar下載
https://download.csdn.net/download/badao_liumang_qizhi/10855459
數據庫配置
1.新建數據庫ssmtest,并新建表user
2.給User表添加字段
3.給user表添加幾條數據
MVC實現
1.在ECclipse下新建MVC結構目錄
?
業務代碼實現
新建pojo
在com.badao.pojo包下新建User.java
package com.badao.pojo;public class User {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}新建mapper
在com.badao.mapper包新建UserMapper.java(接口)
聲明所需的方法,這里只聲明增刪改查
package com.badao.mapper;import java.util.List;import com.badao.pojo.User;public interface UserMapper {public int addUser(User user);public User selectUser(int id);public int updateUser(User user);public void deleteUser(int id);public List<User> selectAllUser(); }再在mapper包下新建UserMapper.xml
其中namespace屬性要與上面所新建的接口一致
id屬性要與上面所建方法名一致
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.badao.mapper.UserMapper"><insert id="addUser" parameterType="User" >insert into user ( name,age ) values (#{name},#{age})??</insert><delete id= "deleteUser" parameterType= "="_int" >delete from user where id= #{id}?</delete><select id="selectUser" parameterType="_int" resultType="User">select * from?? user? where id= #{id}??</select><update id="updateUser" parameterType= "User"><BR>???????????updateusersetname=#{name},age=#{age} where id=#{id}??</update><select id="selectAllUser" resultType="User">select * from?? user????</select>???</mapper>實現Controller
在controller包下新建UserController.java
實現映射簡單的請求路徑listUser并簡單的查詢所有user,然后跳轉到listUser.jsp
package com.badao.controller;import java.util.List;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;import com.badao.pojo.User; import com.badao.service.UserService;// 告訴spring mvc這是一個控制器類 @Controller @RequestMapping("") public class UserController {@AutowiredUserService userService;@RequestMapping("listUser")public ModelAndView listUser(){ModelAndView mav = new ModelAndView();List<User> cs= userService.selectAllUser();// 放入轉發參數mav.addObject("userList", cs);// 放入jsp路徑mav.setViewName("listUser");return mav;}}實現Service
在service包下新建UserService.java(接口)
package com.badao.service;import java.util.List;import com.badao.pojo.User;public interface UserService {List<User> selectAllUser();}再在此包下新建impl包,在impl包下新建UserServiceImpl.java作為前面service的實現lei。
package com.badao.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.badao.mapper.UserMapper; import com.badao.pojo.User; import com.badao.service.UserService; @Service public class UserServiceImpl implements UserService {@AutowiredUserMapper userMapper;public List<User> selectAllUser() {// TODO Auto-generated method stubreturn userMapper.selectAllUser();}}實現jsp頁面
在WEB-INF目錄下新建jsp目錄,再在此目錄下新建jsp目錄,在jsp目錄下新建
listUser.jsp
<%pageContext.setAttribute("APP_PATH", request.getContextPath()); %> <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>查詢所有用戶</title> </head><script type="text/javascript" src="${APP_PATH }/static/js/jquery-1.12.4.min.js"></script> <link href="${APP_PATH }/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet"> <script src="${APP_PATH }/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <body> <table align='center' border='1' cellspacing='0' class="table table-bordered"<th ><td>id</td><td>name</td><td>age</td></th><c:forEach items="${userList}" var="u"><tr class="active"><td >${u.id}</td><td>${u.name}</td><td>${u.age}</td></tr></c:forEach> </table></body> </html>在此頁面引入了bootstrap,用來顯示查詢到的所有user,關于怎樣引入bootstrap
參照
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/85037589
實現SSM整合配置
配置web.xml
通過ContextLoaderListener在web app啟動的時候,獲取contextConfigLocation配置文件的文件名applicationContext.xml,并進行Spring相關初始化工作。
有任何訪問,都被DispatcherServlet所攔截。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="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_2_5.xsd" version="2.5"><!-- spring的配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring mvc核心:分發servlet --><servlet><servlet-name>mvc-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- spring mvc的配置文件 --><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>mvc-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>Spring的配置文件applicationContext.xml
在src目錄下新建applicationContext.xml
1.激活注解
<context:annotation-config />用于激活那些已經在spring容器里注冊過的bean(無論是通過xml的方式還是通過package sanning的方式)上面的注解。
2.組件掃描
?<context:component-scan base-package="com.badao.service" />除了具有<context:annotation-config>的功能之外,<context:component-scan>還可以在指定的package下掃描以及注冊javabean 。
3.配置數據源
將用戶名與密碼改成自己的
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">?<property name="driverClassName">?<value>com.mysql.jdbc.Driver</value>?</property>?<property name="url">?<value>jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8</value>?</property>?<property name="username">?<value>root</value>?</property>?<property name="password">?<value>root</value>?</property>? ? </bean>4.掃描存放SQL語句的UserMapper.xml
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="typeAliasesPackage" value="com.badao.pojo" /><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:com/badao/mapper/*.xml"/></bean>5.掃描Mapper,并將其納入Spring的管理
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.badao.mapper"/></bean>6.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:annotation-config /><context:component-scan base-package="com.badao.service" /><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">?<property name="driverClassName">?<value>com.mysql.jdbc.Driver</value>?</property>?<property name="url">?<value>jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8</value>?</property>?<property name="username">?<value>root</value>?</property>?<property name="password">?<value>523627</value>?</property>? ?</bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="typeAliasesPackage" value="com.badao.pojo" /><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:com/badao/mapper/*.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.badao.mapper"/></bean></beans>配置SpringMVC
src目錄下新建SpringMVC.xml
1.開啟注解驅動<mvc:annotation-driven />
會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,是spring MVC為@Controllers分發請求所必須的。
并提供了:數據綁定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,讀寫XML的支持(JAXB),讀寫JSON的支持(Jackson)。
2.掃描Controller,并將其生命周期納入Spring管理
<context:component-scan base-package="com.badao.controller"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/> </context:component-scan>3.啟用默認處理器,使靜態資源可以訪問
?
<mvc:default-servlet-handler />4.視圖解析器
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /> </bean>5.SpringMVC.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><context:annotation-config/><context:component-scan base-package="com.badao.controller"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><mvc:annotation-driven /><mvc:default-servlet-handler /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean> </beans>項目目錄
?
啟動項目
將項目部署到Tomcat中,打開瀏覽器輸入:
http://127.0.0.1:8080/ssmtest/listUser
項目下載
https://download.csdn.net/download/badao_liumang_qizhi/10856312
?
?
?
總結
以上是生活随笔為你收集整理的快速入门SSM整合配置建立第一个SSM项目模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSM项目中怎样引入并使用Bootstr
- 下一篇: SSM整合时配置文件的编写