springmvc+mybatis整合SSM案例教程
最近一段時間在學習ssm框架,搭建成功的框架沒幾個,不是jar錯誤就是配置檔案內容寫錯,總結了幾點經驗,在這裡跟大家分享一下:
1、jar儘量要統一;(spring使用jar儘量都是同一版本的,其它的也是一樣)
2、XML配置檔案,要仔細編碼,很多的錯誤都是xml引起的,它不像是程式碼片段可除錯,一旦報錯,需要我們仔細排查了
下面說下今天的主題:SSM框架。(在文章最後我會把原始碼放上去,需要的朋友可以做個參考)
先預覽下專案
1、匯入所需jar包
aopalliance-1.0.jar aspectjweaver-1.7.1.jar commons-logging-1.1.3.jar commons-pool-1.6.jar json-lib-2.4-jdk15.jar junit-4.8.2.jar mybatis-3.4.1.jar mybatis-spring-1.3.0.jar ojdbc14.jar spring-aop-4.3.5.RELEASE.jar spring-aspects-4.3.5.RELEASE.jar spring-beans-4.3.5.RELEASE.jar spring-context-4.3.5.RELEASE.jar spring-context-support-4.3.5.RELEASE.jar spring-core-4.3.5.RELEASE.jar spring-expression-4.3.5.RELEASE.jar spring-jdbc-4.3.5.RELEASE.jar spring-tx-4.3.5.RELEASE.jar spring-web-4.3.5.RELEASE.jar spring-webmvc-4.3.5.RELEASE.jar
這裡的jar可分為及部分:
1)、spring所需的jar(這裡我使用的是4.3.5版本的)
2)、使用spring所需jar
3)、mybatis所需jar
4)、連線資料所需jar
5)、Junit所需jar
大致可分為這五部分
2編寫web.xml檔案
這裡的web.xml所需配置並不多,包括兩部分
1)、Springmvc的核心控制器:DispatcherServlet
2)、防止post請求亂碼:CharacterEncodingFilter
完整的配置如下:web.xml
3、編寫applicationContext.xml檔案<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- springmvc前端控制器 --> <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:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</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>*.do</url-pattern> </filter-mapping> </web-app>
具體說明都在註釋裡面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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" default-autowire="byName"> <!-- 配置DataSource資料來源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@192.168.6.25:1521:orcl"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </bean> <!--配置工廠, 建立SqlSessionFactoryBean,同時指定資料來源 ref="dataSource" :指定的是資料來源中的id --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:cn/ssm/entity/empMapper.xml"></property> </bean> <!-- 配置MapperScannerConfigurer value="sqlSessionFactory"指定的是Session工廠的id --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="cn.ssm.dao"></property> </bean> <!-- 開啟註解掃描 --> <context:component-scan base-package="com.ssm"/> <!-- 開啟RequestMapping 註解 --> <mvc:annotation-driven /> <!-- 處理請求轉發 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/emp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
4、實體類emp實體類:
package cn.ssm.entity; import java.sql.Date; public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } }
Condition實體類5、編寫Dao介面package cn.ssm.entity; import java.util.List; public class Condition { private Integer deptno; private Double salary; private List<Integer> empnos; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public List<Integer> getEmpnos() { return empnos; } public void setEmpnos(List<Integer> empnos) { this.empnos = empnos; } }
package cn.ssm.dao; import java.util.List; import cn.ssm.entity.Condition; import cn.ssm.entity.Emp; public interface EmpDao { public List<Emp> findAll(); // 查詢全部 public List<Emp> findByDept(Condition cond); // 根據部門查詢 public List<Emp> findBySalary(Condition cond); // 大於當前工資 public List<Emp> findByDeptAndSalary(Condition cond); // 查詢當前部門下,大於當前收入的員工 public void update(Emp emp);// 更新員工資訊 public List<Emp> findByDeptAndSalary2(Condition cond); // 查詢當前部門下,大於當前收入的員工 public void update2(Emp emp);// 更新員工資訊 public List<Emp> findById(Condition cond); // 根據id查詢 }
6、編寫實體類對映mapper.xml檔案 (與實體類在同一目錄下,從applicationContext.xml檔案中可看出)
empMapper.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.ssm.dao.EmpDao"> <!-- 查詢全部的員工 --> <select id="findAll" resultType="cn.ssm.entity.Emp"> select * from t_emp </select> <!-- if的用法 --> <select id="findByDept" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition"> select * from t_emp <if test="deptno !=null"> where deptno = #{deptno} </if> </select> <!-- chose的用法 --> <select id="findBySalary" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition"> select * from t_emp <choose> <when test="salary>3000"> where sal>#{salary} </when> <otherwise> where sal>3000 </otherwise> </choose> </select> <!-- where --> <!-- 查詢當前部門下,大於當前收入的員工--> <select id="findByDeptAndSalary" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition"> select * from t_emp <where> <if test="deptno !=null"> and deptno=#{deptno} </if> <if test="salary!=null"> and sal>#{salary} </if> </where> </select> <!-- update --> <!-- 更新員工資訊 --> <update id="update" parameterType="cn.ssm.entity.Condition"> update t_emp <set> <if test="ename!=null"> ename=#{ename} </if> <if test="job!=null"> job=#{job} </if> </set> where empno=#{empno} </update> <!-- 使用trim代替where --> <!-- 查詢 --> <select id="findByDeptAndSalary2" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition"> select * from t_emp <trim prefix="where" prefixOverrides="and" > <if test="deptno!=null"> and deptno=#{deptno} </if> <if test="salary !=null"> and sal>#{salary} </if> </trim> </select> <!-- 使用trim代替set --> <!-- 更新 --> <update id="update2"> update t_emp <trim prefix="set" prefixOverrides="," > <if test="ename!=null"> ename=#{ename}, </if> <if test="job!=null"> job=#{job}, </if> </trim> where empno=#{empno} </update> <!-- 根據id查詢 --> <select id="findById" resultType="cn.ssm.entity.Emp" parameterType="cn.ssm.entity.Condition"> select * from t_emp where empno in <foreach collection="empnos" open="(" close=")" separator="," item="id"> #{id} </foreach> </select> </mapper>
7、編寫Junti測試檔案package cn.ssm.test; import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.ssm.dao.EmpDao; import cn.ssm.entity.Condition; import cn.ssm.entity.Emp; public class TestEmp { /** * 查詢全部 */ @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = ctx.getBean(EmpDao.class); List<Emp> list = dao.findAll(); for (Emp e : list) { System.out.println(e.getEmpno() + " " + e.getEname() + " " + e.getJob()); } } /** * 根據部門查詢 */ @Test public void Testfind() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = ctx.getBean(EmpDao.class); Condition condition = new Condition(); condition.setDeptno(10); List<Emp> list = dao.findByDept(condition); for (Emp emp : list) { System.out.println(emp.getEname() + " " + emp.getJob()); } } /** * 查詢大於當前收入的員工 */ @Test public void testfindbysalary() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = ctx.getBean(EmpDao.class); Condition condition = new Condition(); condition.setSalary(4000.0); List<Emp> list = dao.findBySalary(condition); for (Emp emp : list) { System.out.println(emp.getDeptno() + " " + emp.getJob() + " " + emp.getMgr()); } } /** * 查詢當前部門下,大於當前收入的員工 */ public void testfindByDeptAndSalary() { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = context.getBean(EmpDao.class); Condition condition = new Condition(); condition.setDeptno(20); condition.setSalary(2000.0); List<Emp> list = dao.findByDeptAndSalary(condition); for (Emp emp : list) { System.out.println(emp.getEname() + " " + emp.getEname()); } } /** * 查詢當前部門下,大於當前收入的員工 */ @Test public void testfindByDeptAndSalary2() { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = context.getBean(EmpDao.class); Condition condition = new Condition(); condition.setDeptno(20); condition.setSalary(2000.0); List<Emp> list = dao.findByDeptAndSalary(condition); for (Emp emp : list) { System.out.println(emp.getEname() + " " + emp.getEname()); } } /** * 更新員工資訊 */ public void testupdate() { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = context.getBean(EmpDao.class); Emp emp = new Emp(); emp.setEmpno(14); emp.setEname("Tom"); dao.update(emp); } /** * 更新員工資訊 */ public void testupdate2() { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = context.getBean(EmpDao.class); Emp emp = new Emp(); emp.setEmpno(14); emp.setEname("Tom"); dao.update(emp); } /** * 根據id查詢 */ @Test public void testFindById() { ApplicationContext axt = new ClassPathXmlApplicationContext( "applicationContext.xml"); EmpDao dao = axt.getBean(EmpDao.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(3); ids.add(10); Condition cond = new Condition(); cond.setEmpnos(ids); List<Emp> list = dao.findById(cond); for (Emp emp : list) { System.out.println(emp.getEname() + " " + emp.getDeptno()); } } }
8、編寫Controller進行測試
package cn.ssm.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.ssm.dao.EmpDao; import cn.ssm.entity.Emp; @Controller public class EmpController { @Resource private EmpDao empDao; public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } @RequestMapping("/findEmp.do") public String find(Model model) { List<Emp> list = empDao.findAll(); model.addAttribute("emps", list); return "emp_list"; } }
9、編寫jsp頁面
根據applicationContext.xml中的配置,我們在WEB-INF中建立emp資料夾,然後建立emp_list.jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'emp_list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body style="text-align: center;"> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr> <th>EMPNO</th> <th>ENAME</th> <th>JOB</th> <th>MGR</th> <th>HIREDATE</th> <th>SAL</th> <th>COMM</th> <th>DEPTNO</th> </tr> <c:forEach items="${emps}" var="emp"> <tr> <td>${emp.empno}</td> <td>${emp.ename}</td> <td>${emp.job}</td> <td>${emp.mgr}</td> <td>${emp.hiredate}</td> <td>${emp.sal}</td> <td>${emp.comm}</td> <td>${emp.deptno}</td> </tr> </c:forEach> </table> </body> </html>
10、測試在瀏覽器中輸入:http://localhost:8080/SSMDemo/findEmp.do即可
(專案預覽圖中有個db.properties沒有用到,
<!-- 讀取屬性檔案 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties"></property> </bean> <!-- 配置DataSource資料來源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean>
把applicationContext.xml修改成這樣就可以了。兩種方式都可以,有問題可以在文章末尾處瀏覽,大家一起學習)
相關推薦
springmvc+mybatis整合SSM案例教程
最近一段時間在學習ssm框架,搭建成功的框架沒幾個,不是jar錯誤就是配置檔案內容寫錯,總結了幾點經驗,在這裡跟大家分享一下:1、jar儘量要統一;(spring使用jar儘量都是同一版本的,其它的也是一樣)2、XML配置檔案,要仔細編碼,很多的錯誤都是xml引起的,它不像是
Spring + SpringMVC + Mybatis整合SSM框架配置方法
剛學完SSM框架的整合,想找個專案玩玩,網上一搜資源一大把,把專案直接搬進開發工具裡,總是遇到各種各樣的問題,一方面是專案本身需要結合環境重新配置,一方面自己沒辦法理解別人的思想。不如從頭開始梳理一下SSM框架的配置整合方法,溫故而知新! 1. 配置專案物件模
SSM springmvc mybatis 整合 bootstrap maven shiro druid ehcache SSM框架源碼
微信開發 springmvc mybatis java 公眾平臺 官網 http://www.fhadmin.org/ A 調用攝像頭拍照,自定義裁剪編輯頭像,頭像圖片色度調節 B 集成代碼生成器 [正反雙向](單表、主表、明細表、樹形表,快速開發利器)+快速表單構建器 fre
spring,springmvc,mybatis整合ssm框架出現ORA-02289:序列不存在問題
sel 請求 開始 color 九九 pri soft 框架 服務 今天整合了一個SSM項目,完了後部署到Tomcat服務器,正常啟動。但是當我發送請求時,報錯,,如下 報錯說序列不存在,可是我明明創建了序列呀,然後我測試了一下,測試語句:select tb_user_s
Intellij IDEA使用(十二)—— 使用Intellij IDEA匯入Eclipse建立的Spring+SpringMVC+Mybatis(SSM)整合專案並修改相關配置
1、匯入專案 File → Open → 選擇專案所在的目錄開啟專案 3、下面是新增的需要配置的地方 File → Project Structure → 選擇Facets ,新增Spring
Spring-SpringMVC-Mybatis整合的步驟
read oot exceptio img gda gif force system 緩存 1.導入jar包 1.1 spring面向切面jar包 com.springsource.net.sf.cglib-2.2.0.jar com.spring
spring+springmvc+mybatis整合
聲明 參數 stdout 加載 strong 相關 version pts check 搭建ssm框架,我們要分幾步進行,把每個配置文件分開寫,這樣看上去一目了然,有利於後期的修改維護,對自己也可以記請每一步的內容和步驟,方便記憶 一.spring-dao.xml 二.jd
spring和mybatis整合小案例
http myba 效果展示 圖片 tro col mybatis 登錄模塊 -c 案例效果展示: 登錄模塊: 用戶註冊模塊: spring和mybatis整合小案例
Spring4+SpringMVC+MyBatis整合思路
容器 getc lte nal 自定義 object eof supported instance 本文主要簡單講解框架整合的思路。1、Spring框架的搭建 這個很簡單,只需要web容器中註冊org.springframework.web.context.ContextL
spring+springmvc+mybatis整合詳解
一,導包 1,建立一個web工程,匯入spring,springmvc,mybatis需要的jar包 2,下載適配包Mybatis-Spring.jar(或者寫依賴),不同版本的spring和mybatis需要的適配包不同 二,web.xml (在WEB-INF目錄下) 1,載
maven中spring+springmvc+mybatis整合詳細配置
首先配置mybatis,可以參考這篇部落格的前期基礎配置使用。 https://blog.csdn.net/qq_41520636/article/details/84146699 下面配置結構: 需要的jar,在pom.xml中引入 <!--統一j
spring + springmvc + mybatis整合以及一些常見問題(二)
建立entity(實體類) 建立dao 對資料庫進行操作 在main/resources中建立一個包 裡面放Mapper <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper P
spring + springmvc + mybatis整合以及一些常見問題(一)
寫此文章提醒下自己 下次記得配置web.xml 首先導包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifa
(實用篇)一步步搭建Spring+SpringMVC+MyBatis(SSM)框架
一、前言 本篇內容是培訓作業的第一個任務,旨在搭建一個SSM框架,做一個HTML頁面,能查詢,能插入資料(新建使用者)、登陸。實現的功能最為基礎,但是要求所有程式碼均為手寫,不能使用Mybatis 自動生成mapping和dao。由於此框架是後面一系列任務的基礎,所以,還是
SpringMVC Mybatis整合
工程目錄樹 工程裡共2個配置檔案,分別是spring.xml,是spring和mybatis的配置檔案,還有一個是springMVC的配置檔案,此外有2個資原始檔:jdbc.propertis和log4j.properties. 步驟 建立Maven工程
【框架整合】spring+springmvc+mybatis整合
Spingmvc +mybatis整合 思路 第一步:整合dao層 mybatis和spring整合,通過spirng管理mapper介面。 使用mapper掃描器自動掃描mapper介面在spring中進行註冊。 第二步:整合serv
Spring+SpringMVC+MyBatis整合配置檔案的搭建
SSM專案的環境搭建: 一、專案的目錄結構如下: 二、匯入的jar包:WEB-INF/lib下 三、resources下的幾個配置檔案+WEB-INF下的web.xml檔案需要自己配置: wei.xml: <?xml version="1.0" en
基於maven搭建spring+springMVC+mybatis(SSM)框架專案
一.簡介 這篇文章是記錄我自己手動搭建基於maven的SSM(spring+springMVC+mybatis)框架專案的整個過程,目的是為了加深印象和方便以後查閱以及整理思路。 二.開發環境準備 (1)系統:Windows10(專業版) (2)eclispe版本:Eclipse J
小練習:Spring + SpringMVC + Mybatis 整合
注:將涉及的類(包、模組)交給Spring容器管理後,可在開發過程中注入需要的東西,通過Spring容器的管理,避免了各種類及其屬性的額外宣告使用,簡化開發流程 web.xml中,可以通過<context-param>批量把各種xml裡配置好的Bean一起載入到Spring容器中
簡單的SpringMVC+Mybatis整合
先申明幾點: 1.這個整合呢,我也是看的其他大神的各種資料來學習的。 2.SpringMVC框架的原理、能做什麼、有什麼長短處什麼的,我這邊就不寫了,我相信你在學這個框架的時候,你肯定得先去了解。 3.我儘量簡短的寫,然後呢,我儘可能的把已經整合好的框架原始碼丟上來,到時候你們下載下去自己