1. 程式人生 > 其它 >SSM整合(spring-springmvc-mybatis)之CRUD

SSM整合(spring-springmvc-mybatis)之CRUD

概述:

加入功能:CRUD、分頁、返回json、國際化

配置了訪問靜態檔案;

配置了REST 風格的程式碼;

一、目錄結構:

目錄:

     

jar包說明:

spring核心jar包:

  1. commons-logging-1.2.jar
  2. spring-beans-5.3.10.jar
  3. spring-context-5.3.10.jar
  4. spring-core-5.3.10.jar
  5. spring-expression-5.3.10.jar

spring aop:

  1. spring-aop-5.3.10.jar
  2. spring-aspects-5.3.10.jar

springmvc jar包:

  1. spring-web-5.3.10.jar
  2. spring-webmvc-5.3.10.jar

spring 資料庫互動jar包:

  1. spring-jdbc-5.3.10.jar
  2. spring-tx-5.3.10.jar

mybatis jar包:

  1. mybatis-3.5.9.jar
  2. mysql-connector-java-8.0.27 (1).jar
  3. log4j-1.2.17.jar

mybatis-spring 介面卡包:

  1. mybatis-spring-1.3.2.jar

c3po:

  1. c3p0-0.9.5.5.jar
  2. mchange-commons-java-0.2.19.jar

jstl:

  1. standard.jar
  2. jstl.jar

jackson(目的是返回json):

  1. jackson-annotations-2.9.10.jar
  2. jackson-core-2.9.10.jar
  3. jsqlparser-4.3.jar

mybatis 分頁:

  1. jsqlparser-4.3.jar
  2. pagehelper-5.3.0.jar

二、java原始碼

實體類:

普通實體類可以繼承通用類,通用類提供了分頁所需要的欄位;原因是mybatis分頁外掛pageHelper的使用,這裡採用了引數物件分頁 。所以需要在物件中提供分頁欄位,比如:pageNum、pageSize、orderBy;

通用類CommonEntity.java

 1
package ssm.entity; 2 3 public class CommonEntity { 4 private Integer pageNum; 5 private Integer pageSize; 6 private String orderBy = "id desc"; 7 8 public Integer getPageNum() { 9 return pageNum; 10 } 11 12 public void setPageNum(Integer pageNum) { 13 this.pageNum = pageNum; 14 } 15 16 public Integer getPageSize() { 17 return pageSize; 18 } 19 20 public void setPageSize(Integer pageSize) { 21 this.pageSize = pageSize; 22 } 23 24 public String getOrderBy() { 25 return orderBy; 26 } 27 28 public void setOrderBy(String orderBy) { 29 this.orderBy = orderBy; 30 } 31 32 }
View Code

實體類:Employee.java:

package ssm.entity;

public class Employee extends CommonEntity {

    private Integer id;
    private String lastName;
    private String email;
    private String gender;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
    }

}
View Code

Controller部分:

 1 package ssm.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 import org.springframework.web.servlet.ModelAndView;
10 
11 import com.github.pagehelper.PageInfo;
12 
13 import ssm.entity.Employee;
14 import ssm.service.EmployeeService;
15 
16 /**
17  * @author lixiuming
18  * @Description
19  * @Date 2022年3月29日下午3:38:06
20  */
21 @RequestMapping("/emp")
22 @Controller
23 public class EmployeeController {
24     @Autowired
25     private EmployeeService employeeService;
26 
27     /**
28      * 列表頁面
29      * 
30      * @param employee
31      * @return
32      */
33     @RequestMapping("/list")
34     public ModelAndView list(Employee employee) {
35         ModelAndView mv = new ModelAndView("index");
36         PageInfo<Employee> list = employeeService.list(employee);
37         mv.addObject("pages", list);
38         return mv;
39     }
40 
41     /**
42      * 新增頁面
43      * 
44      * @return
45      */
46     @RequestMapping("/add")
47     public ModelAndView add() {
48         ModelAndView mv = new ModelAndView("update");
49         return mv;
50     }
51 
52     /**
53      * 編輯頁面
54      * 
55      * @param id
56      * @return
57      */
58     @RequestMapping(value = "/edit/{id}")
59     public ModelAndView edit(@PathVariable(name = "id", required = false) Integer id) {
60         Employee employee = employeeService.getEmpById(id);
61         ModelAndView mv = new ModelAndView("update");
62         mv.addObject("employee", employee);
63         return mv;
64     }
65 
66     /**
67      * 新增/編輯
68      * 
69      * @param employee
70      * @return
71      */
72     @ResponseBody
73     @RequestMapping(value = "/update", method = RequestMethod.POST)
74     public String update(Employee employee) {
75         System.out.println(employee);
76         return employeeService.update(employee);
77     }
78 
79     /**
80      * 詳情
81      * 
82      * @param id
83      * @return
84      */
85     @ResponseBody
86     @RequestMapping(value = "/detail", method = RequestMethod.POST)
87     public Employee update(Integer id) {
88         return employeeService.getEmpById(id);
89     }
90 
91 }
View Code

Service部分:

service介面:

 1 package ssm.service;
 2 
 3 import com.github.pagehelper.PageInfo;
 4 
 5 import ssm.entity.Employee;
 6 
 7 /**
 8  * 
 9  * @author lixiuming
10  * @Description
11  * @Date 2022年3月29日下午3:46:33
12  */
13 public interface EmployeeService {
14 
15     /**
16      * 根據ID獲取實體
17      * 
18      * @param id
19      * @return
20      */
21     Employee getEmpById(Integer id);
22 
23     /**
24      * 獲取列表頁面
25      * 
26      * @param employee
27      * @return
28      */
29     PageInfo<Employee> list(Employee employee);
30 
31     /**
32      * 新增/修改
33      * 
34      * @param employee
35      * @return
36      */
37     String update(Employee employee);
38 
39 }
View Code

serviceImple實現類:

 1 package ssm.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import com.github.pagehelper.PageInfo;
 7 
 8 import ssm.entity.Employee;
 9 import ssm.mapper.EmployeeMapper;
10 
11 @Service("employeeService")
12 public class EmployeeServiceImpl implements EmployeeService {
13 
14     @Autowired
15     private EmployeeMapper employeeMapper;
16 
17     @Override
18     public Employee getEmpById(Integer id) {
19         return employeeMapper.getEmpById(id);
20     }
21 
22     @Override
23     public PageInfo<Employee> list(Employee employee) {
24         return employeeMapper.list(employee);
25     }
26 
27     @Override
28     public String update(Employee employee) {
29         if (employee.getId() != null && employee.getId() > 0) {
30             employeeMapper.update(employee);
31         } else {
32             employeeMapper.insert(employee);
33         }
34         return "200";
35     }
36 
37 }
View Code

Mapper部分:

 1 package ssm.mapper;
 2 
 3 import com.github.pagehelper.PageInfo;
 4 
 5 import ssm.entity.Employee;
 6 
 7 /**
 8  * 
 9  * @author lixiuming
10  * @Description
11  * @Date 2022年3月29日下午3:48:45
12  */
13 public interface EmployeeMapper {
14 
15     /**
16      * 根據id 獲取 實體物件
17      * 
18      * @param id
19      * @return
20      */
21     Employee getEmpById(Integer id);
22 
23     /**
24      * 獲取頁面
25      * 
26      * @param employee
27      * @return
28      */
29     PageInfo<Employee> list(Employee employee);
30 
31     /**
32      * 插入資料
33      * 
34      * @param employee
35      */
36     void insert(Employee employee);
37 
38     /**
39      * 修改
40      * 
41      * @param employee
42      */
43     void update(Employee employee);
44 }
View Code

sql語句:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="ssm.mapper.EmployeeMapper">
 6 
 7     <!-- 根據id 查詢資料資料 -->
 8     <select id="getEmpById" resultType="ssm.entity.Employee">
 9         select * from tbl_employee
10         where id=#{id,jdbcType=INTEGER}
11     </select>
12     <!-- 查詢列表 -->
13     <select id="list" resultType="ssm.entity.Employee">
14         select id,last_name as
15         lastName,email,gender from tbl_employee
16     </select>
17     <!-- 插入 -->
18     <insert id="insert" useGeneratedKeys="true">
19         insert into tbl_employee
20         (last_name,email,gender) values(
21         #{lastName,jdbcType=VARCHAR},
22         #{email,jdbcType=VARCHAR},
23         #{gender,jdbcType=VARCHAR}
24 
25         )
26     </insert>
27     <!-- 修改 -->
28     <update id="update">
29         update tbl_employee set
30         last_name=#{lastName,jdbcType=VARCHAR},email=#{email,jdbcType=VARCHAR},gender=#{gender,jdbcType=VARCHAR}
31         where id=#{id,jdbcType=INTEGER}
32     </update>
33 </mapper>
View Code

資料轉換:

StringToDateConverter, 把String 轉為日期;

應用場景:form表單提交日期到後臺,由於傳輸的資料時String型別的日期,而後臺對應的欄位為java.util.Date型別;如果需要實現後臺傳入的資料自動轉成對應的java.util.Date型別的欄位則需要配置配置資料轉換;轉換類如下:

 1 package ssm.convert;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.springframework.core.convert.converter.Converter;
 7 
 8 public class StringToDateConverter implements Converter<String, Date> {
 9 
10     // 日期型別模板:如yyyy-MM-dd,通過配置時注入
11     private String datePattern;
12 
13     public void setDatePattern(String datePattern) {
14         this.datePattern = datePattern;
15     }
16 
17     // Converter<S,T>介面的型別轉換方法
18     @Override
19     public Date convert(String date) {
20         try {
21             SimpleDateFormat dateFormat = new SimpleDateFormat(this.datePattern);
22             // 將日期字串轉換成Date型別返回
23             return dateFormat.parse(date);
24         } catch (Exception e) {
25             e.printStackTrace();
26             System.out.println("日期轉換失敗!");
27             return null;
28         }
29 
30     }
31 
32 }
View Code

StringToIntegerConverter,把String轉成Integer

 1 package ssm.convert;
 2 
 3 import org.springframework.core.convert.converter.Converter;
 4 
 5 public class StringToIntegerConverter implements Converter<String, Integer> {
 6 
 7     // Converter<S,T>介面的型別轉換方法
 8     @Override
 9     public Integer convert(String intData) {
10         try {
11             // 將日期字串轉換成Date型別返回
12             return Integer.valueOf(intData);
13         } catch (Exception e) {
14             e.printStackTrace();
15             System.out.println("整型轉換失敗");
16             return null;
17         }
18 
19     }
20 
21 }
View Code

分頁類:

參考地址:https://github.com/xjs1919/PageHelperExt

PageObjectFactory.java

 1 package ssm.utils;
 2 
 3 import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
 4 
 5 import com.github.pagehelper.PageInfo;
 6 
 7 /**
 8  * Created by xujiashuai on 2016/6/29.
 9  */
10 public class PageObjectFactory extends DefaultObjectFactory {
11 
12     private static final long serialVersionUID = 3963031299778136554L;
13 
14     /**
15      * 如果型別為page的話讓其也判斷為集合
16      */
17     public <T> boolean isCollection(Class<T> type) {
18         if (type == PageInfo.class) {
19             return true;
20         }
21         return super.isCollection(type);
22     }
23 
24     @SuppressWarnings({ "unchecked", "rawtypes" })
25     public <T> T create(Class<T> type) {
26         if (type == PageInfo.class) {
27             return (T) new PageInfo();
28         }
29         return create(type, null, null);
30     }
31 }
View Code

PageObjectWrapper.java

  1 package ssm.utils;
  2 
  3 import java.util.List;
  4 
  5 import org.apache.ibatis.reflection.MetaObject;
  6 import org.apache.ibatis.reflection.factory.ObjectFactory;
  7 import org.apache.ibatis.reflection.property.PropertyTokenizer;
  8 import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
  9 
 10 import com.github.pagehelper.Page;
 11 import com.github.pagehelper.PageInfo;
 12 
 13 public class PageObjectWrapper implements ObjectWrapper {
 14 
 15     @SuppressWarnings("rawtypes")
 16     private PageInfo pageinfo;
 17 
 18     @SuppressWarnings("rawtypes")
 19     public PageObjectWrapper(PageInfo wrapped) {
 20         this.pageinfo = wrapped;
 21     }
 22 
 23     @SuppressWarnings({ "rawtypes", "unchecked" })
 24     @Override
 25     public <E> void addAll(List<E> element) {
 26         Page page = (Page) element;
 27         pageinfo.setPageNum(page.getPageNum());
 28         pageinfo.setPageSize(page.getPageSize());
 29         pageinfo.setPages(page.getPages());
 30         pageinfo.setList(page);
 31         pageinfo.setSize(page.size());
 32         pageinfo.setTotal(page.getTotal());
 33         if (page.size() == 0) {
 34             pageinfo.setStartRow(0);
 35             pageinfo.setEndRow(0);
 36         } else {
 37             pageinfo.setStartRow(page.getStartRow() + 1);
 38             pageinfo.setEndRow(pageinfo.getStartRow() - 1 + pageinfo.getSize());
 39         }
 40         pageinfo.setNavigatePages(5);
 41         calcNavigatepageNums(pageinfo, page);
 42         calcPage(pageinfo);
 43         judgePageBoudary(pageinfo);
 44     }
 45 
 46     @SuppressWarnings("rawtypes")
 47     private void calcNavigatepageNums(PageInfo pageinfo, com.github.pagehelper.Page page) {
 48         int pages = pageinfo.getPages();
 49         int navigatePages = pageinfo.getNavigatePages();
 50         int pageNum = pageinfo.getPageNum();
 51         int[] navigatepageNums = null;
 52         if (pages <= navigatePages) {
 53             navigatepageNums = new int[pages];
 54             for (int i = 0; i < pages; i++) {
 55                 navigatepageNums[i] = i + 1;
 56             }
 57         } else {
 58             navigatepageNums = new int[navigatePages];
 59             int startNum = pageNum - navigatePages / 2;
 60             int endNum = pageNum + navigatePages / 2;
 61 
 62             if (startNum < 1) {
 63                 startNum = 1;
 64                 for (int i = 0; i < navigatePages; i++) {
 65                     navigatepageNums[i] = startNum++;
 66                 }
 67             } else if (endNum > pages) {
 68                 endNum = pages;
 69                 for (int i = navigatePages - 1; i >= 0; i--) {
 70                     navigatepageNums[i] = endNum--;
 71                 }
 72             } else {
 73                 for (int i = 0; i < navigatePages; i++) {
 74                     navigatepageNums[i] = startNum++;
 75                 }
 76             }
 77         }
 78         pageinfo.setNavigatepageNums(navigatepageNums);
 79     }
 80 
 81     @SuppressWarnings("rawtypes")
 82     private void calcPage(PageInfo pageinfo) {
 83         int[] navigatepageNums = pageinfo.getNavigatepageNums();
 84         int pageNum = pageinfo.getPageNum();
 85         if (navigatepageNums != null && navigatepageNums.length > 0) {
 86             if (pageNum > 1) {
 87                 pageinfo.setPrePage(pageNum - 1);
 88             }
 89             if (pageNum < pageinfo.getPages()) {
 90                 pageinfo.setNextPage(pageNum + 1);
 91             }
 92         }
 93     }
 94 
 95     @SuppressWarnings("rawtypes")
 96     private void judgePageBoudary(PageInfo pageinfo) {
 97         int pageNum = pageinfo.getPageNum();
 98         int pages = pageinfo.getPages();
 99         pageinfo.setIsFirstPage(pageNum == 1);
100         pageinfo.setIsLastPage(pageNum == pages);
101         pageinfo.setHasPreviousPage(pageNum > 1);
102         pageinfo.setHasNextPage(pageNum < pages);
103     }
104 
105     @Override
106     public Object get(PropertyTokenizer prop) {
107         return null;
108     }
109 
110     @Override
111     public void set(PropertyTokenizer prop, Object value) {
112 
113     }
114 
115     @Override
116     public String findProperty(String name, boolean useCamelCaseMapping) {
117         return null;
118     }
119 
120     @Override
121     public String[] getGetterNames() {
122         return new String[0];
123     }
124 
125     @Override
126     public String[] getSetterNames() {
127         return new String[0];
128     }
129 
130     @Override
131     public Class<?> getSetterType(String name) {
132         return null;
133     }
134 
135     @Override
136     public Class<?> getGetterType(String name) {
137         return null;
138     }
139 
140     @Override
141     public boolean hasSetter(String name) {
142         return false;
143     }
144 
145     @Override
146     public boolean hasGetter(String name) {
147         return false;
148     }
149 
150     @Override
151     public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
152         return null;
153     }
154 
155     @Override
156     public boolean isCollection() {
157         return false;
158     }
159 
160     @Override
161     public void add(Object element) {
162 
163     }
164 }
View Code

PageObjectWrapperFactory.java

 1 package ssm.utils;
 2 
 3 import org.apache.ibatis.reflection.MetaObject;
 4 import org.apache.ibatis.reflection.ReflectionException;
 5 import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
 6 import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
 7 
 8 import com.github.pagehelper.PageInfo;
 9 
10 /**
11  * Created by xujiashuai on 2016/6/29.
12  */
13 public class PageObjectWrapperFactory extends DefaultObjectWrapperFactory {
14 
15     @Override
16     public boolean hasWrapperFor(Object object) {
17         if (object instanceof PageInfo) {
18             return true;
19         }
20         return false;
21     }
22 
23     @SuppressWarnings("rawtypes")
24     @Override
25     public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
26         if (object instanceof PageInfo) {
27             return new PageObjectWrapper((PageInfo) object);
28         }
29         throw new ReflectionException(
30                 "The DefaultObjectWrapperFactory should never be called to provide an ObjectWrapper.");
31     }
32 }
View Code

三、配置

web.xml配置:

DispatcherServlet(分發器)配置 + ContextLoaderListener整合spring配置+CharacterEncodingFilter請求/響應編碼配置;

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5     version="4.0">
 6     <filter>
 7         <filter-name>CharacterEncodingFilter</filter-name>
 8         <filter-class>org.springframework.web.filter.CharacterEncodingFilter
 9         </filter-class>
10         <init-param>
11             <param-name>encoding</param-name>
12             <param-value>utf-8</param-value>
13         </init-param>
14     </filter>
15     <filter-mapping>
16         <filter-name>CharacterEncodingFilter</filter-name>
17         <url-pattern>/*</url-pattern>
18     </filter-mapping>
19     <context-param>
20         <param-name>contextConfigLocation</param-name>
21         <param-value>classpath:spring.xml</param-value>
22     </context-param>
23     <listener>
24         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
25     </listener>
26     <!-- 配置DispatcherServlet(快捷鍵 alt +/) -->
27     <servlet>
28         <servlet-name>springDispatcherServlet</servlet-name>
29         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
30         <!-- 配置DispatcherServletd 一個初始化引數:配置springmvc配置檔案的位置和名稱 -->
31         <!-- 實際上也可以不通過 contextConfigLocation 來配置Springmvc的配置檔案,而是用預設的 即預設的配置檔案為 
32             /WEB-INF/<servlet-name>-servlet.xml 本專案預設位置配置檔案即為: /WEB-INF/springDispatcherServlet-servlet.xml -->
33         <init-param>
34             <param-name>contextConfigLocation</param-name>
35             <param-value>classpath:springmvc.xml</param-value>
36         </init-param>
37         <!-- 表示springDispatcherServlet在載入的時候被建立 -->
38         <load-on-startup>1</load-on-startup>
39     </servlet>
40 
41     <!-- Map all requests to the DispatcherServlet for handling -->
42     <servlet-mapping>
43         <servlet-name>springDispatcherServlet</servlet-name>
44         <url-pattern>/</url-pattern>
45     </servlet-mapping>
46 </web-app>
View Code

springmvc配置:

springmvc 只負責網站跳轉邏輯;

springmvc.xml:

  • spring資料轉換:配置了String轉 java.util.Date ,實現類是StringToDateConverter配置了 String 轉Integer ,實現類是StringToIntegerConverter
    完成後需要加到<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"></mvc:annotation-driven>中;
  • <mvc:default-servlet-handler /> 的作用是可以訪問到靜態檔案,比如訪問js;如果沒有這個配置,頁面無法引用js;

參考:https://www.cnblogs.com/lixiuming521125/p/16011536.html

 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"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <!-- spirngmvc 只負責 網站跳轉邏輯 -->
11     <!-- 配置掃描的包:只掃描Controller 和 Service -->
12     <context:component-scan base-package="ssm"
13         use-default-filters="false">
14         <context:include-filter type="annotation"
15             expression="org.springframework.stereotype.Controller" />
16         <context:include-filter type="annotation"
17             expression="org.springframework.stereotype.Service" />
18     </context:component-scan>
19     <!-- 配置檢視解析器 解析檢視地址: /WEB-INF/views/+檢視名稱+.jsp -->
20     <bean
21         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22         <property name="prefix" value="/WEB-INF/views/"></property>
23         <property name="suffix" value=".jsp"></property>
24     </bean>
25 
26 
27     <!--配置SpringMVC的資料轉換器 -->
28     <bean id="conversionServiceFactoryBean"
29         class="org.springframework.context.support.ConversionServiceFactoryBean">
30         <property name="converters">
31             <list>
32                 <bean class="ssm.convert.StringToDateConverter">
33                     <property name="datePattern" value="yyyy-MM-dd"></property>
34                 </bean>
35                 <bean class="ssm.convert.StringToIntegerConverter">
36                 </bean>
37             </list>
38         </property>
39     </bean>
40 
41     <!--配置conversion-service屬性 指向SpringMVC的資料轉換器 -->
42     <mvc:annotation-driven
43         conversion-service="conversionServiceFactoryBean">
44     </mvc:annotation-driven>
45 
46     <!-- 配置訪問靜態資源 -->
47     <mvc:default-servlet-handler />
48 
49     <!-- 配置國際化檔案 -->
50     <bean id="messageSource"
51         class="org.springframework.context.support.ResourceBundleMessageSource">
52         <property name="basename" value="i18n"></property>
53     </bean>
54 
55 
56 </beans>

spring配置:

spring 希望管理所有的業務邏輯元件,等

spring.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"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 8         http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
11 
12     <!-- spring 希望管理所有的業務邏輯元件,等 -->
13 
14     <!-- 自動掃描配置 除了掃描Controller 和 Service 外掃描所有其他註解標記的類 -->
15     <context:component-scan base-package="ssm"
16         use-default-filters="false">
17         <context:exclude-filter type="annotation"
18             expression="org.springframework.stereotype.Controller" />
19         <context:exclude-filter type="annotation"
20             expression="org.springframework.stereotype.Service" />
21     </context:component-scan>
22 
23     <!-- spring用來控制業務邏輯。資料來源、事務控制、aop... -->
24     <!-- 匯入資原始檔 -->
25     <context:property-placeholder
26         location="classpath:dbconfig.properties" />
27     <!-- 配置c3p0資料來源 -->
28     <bean id="datasource"
29         class="com.mchange.v2.c3p0.ComboPooledDataSource">
30         <property name="user" value="${jdbc.username}"></property>
31         <property name="password" value="${jdbc.password}"></property>
32         <property name="driverClass" value="${jdbc.driver}"></property>
33         <property name="jdbcUrl" value="${jdbc.url}"></property>
34     </bean>
35     <!-- spring 事務管理器 -->
36     <bean id="transactionManager"
37         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
38         <property name="dataSource" ref="datasource"></property>
39     </bean>
40     <!-- 開啟基於註解的事務 -->
41     <tx:annotation-driven
42         transaction-manager="transactionManager" />
43 
44     <!-- 整合mybatis目的: 1.spring管理所有元件。mapper的實現類。可以對service中引用dao的內容,可以為@Autowired 
45         自動注入mapper; 2.spring用來管理宣告式事務, -->
46     <!-- 創建出 sqlSessionFactoryBean物件 -->
47     <bean id="sqlSessionFactoryBean"
48         class="org.mybatis.spring.SqlSessionFactoryBean">
49         <property name="dataSource" ref="datasource"></property>
50         <!-- configLocation指定全域性配置檔案的位置 -->
51         <property name="configLocation"
52             value="classpath:mybatis-config.xml"></property>
53         <!-- 指定mapper位置(如果mapper與mapper介面的檔名不一樣的話這裡需要配置,若一樣就可以不配置) -->
54         <property name="mapperLocations"
55             value="classpath:mapper/*.xml"></property>
56         <property name="plugins">
57             <array>
58                 <bean class="com.github.pagehelper.PageInterceptor">
59                     <property name="properties">
60                         <!--使用下面的方式配置引數,一行配置一個 -->
61                         <value>
62                             helperDialect=mysql
63                             reasonable=true
64                             supportMethodsArguments=true
65                             params=count=countSql
66                             autoRuntimeDialect=true
67                         </value>
68                     </property>
69                 </bean>
70             </array>
71         </property>
72     </bean>
73     <!--配置一個可以進行批量執行的sqlSession -->
74     <bean id="sqlSession"
75         class="org.mybatis.spring.SqlSessionTemplate">
76         <constructor-arg name="sqlSessionFactory"
77             ref="sqlSessionFactoryBean"></constructor-arg>
78         <constructor-arg name="executorType" value="BATCH"></constructor-arg>
79     </bean>
80     <!-- 掃描所有的mapper介面的實現,讓這些mapper能夠自動注入 -->
81     <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 配置SqlSessionFactoryBean的名稱 
82         <property name="basePackage" value="ssm.mapper" /> </bean> -->
83     <mybatis-spring:scan base-package="ssm.mapper" />
84 </beans>

mybatis-config.xml配置:

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- settings包含很多重要的設定項; name:設定項的名字,value設定項的取值 -->
 7     <settings>
 8         <!-- 開啟駝峰命名 比如資料庫欄位為Last_Name 則 自動變為lastName -->
 9         <setting name="mapUnderscoreToCamelCase" value="true" />
10         <!-- 分步查詢配合一下兩個配置 實現懶載入(按需載入) -->
11         <setting name="lazyLoadingEnabled" value="true" />
12         <setting name="aggressiveLazyLoading" value="false" />
13     </settings>
14     <objectFactory
15         type="ssm.utils.PageObjectFactory" />
16 
17     <objectWrapperFactory
18         type="ssm.utils.PageObjectWrapperFactory" />
19 
20     <!-- 配置順序,properties settings typeAliases typeHandlers objectFactory objectWrapperFactory 
21         reflectFactory plugings environment databaseIdProvider mappers -->
22 </configuration>

 

 

 

dbconfig.properties:

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/mybatis
3 jdbc.username=root
4 jdbc.password=
5 
6 jdbc2.driver=com.mysql.jdbc.Driver
7 jdbc2.url=jdbc:mysql://localhost:3306/mybatis2
8 jdbc2.username=root
9 jdbc2.password=
View Code

i18n_zh_CN.properties

 1 EMPLOYEE.LASTNAME=姓名
 2 EMPLOYEE.EMAIL=郵箱
 3 EMPLOYEE.GENDER=性別
 4 EMPLOYEE.LIST=列表
 5 EMPLOYEE.EDIT=新增/修改
 6 SYS.OPERATE=操作
 7 SYS.ADD=新增
 8 SYS.EDIT=修改
 9 SYS.SUBMIT=提交
10 SYS.DETAIL=詳情
View Code

執行結果:

訪問:http://localhost:8080/emp/list?pageNum=1&pageSize=5,顯示列表第一頁,資料條數為5條;

訪問:http://localhost:8080/emp/list?pageNum=2&pageSize=5,顯示列表第二頁,資料條數為5條;

目前是id 倒敘的,也可以自定義排序規則;比如:http://localhost:8080/emp/list?pageNum=2&pageSize=5&orderBy=id%20asc,lastName%20asc;

  

 點選新增跳轉到跳轉頁面;點選修改操作,顯示修改頁面;

  

 

 點選詳情alert彈出詳情;