SSM整合步骤
1、先在mysql-front中创建表,准备数据
2、新建动态web项目ssm,新建包、类、xml、jsp文件(如图),并导入jar包
3、pojo Category
package com.how2java.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
mapper CategoryMapper
package com.how2java.mapper;
import java.util.List;
import com.how2java.pojo.Category;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public List<Category> list();
public int count();
Category.xml(跟CategoryMapper目录下,个人理解:可以看到这里含数据库的基本操作增删改查)
<?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="com.how2java.mapper.CategoryMapper">
<insert id="add" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="delete" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="get" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="update" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="list" resultType="Category">
select * from category_
</select>
</mapper>
CategoryService(注意:这里不是class,而是interface)
package com.how2java.service;
import java.util.List;
import com.how2java.pojo.Category;
public interface CategoryService {
List<Category> list();
}
CategoryServiceImpl,可以看到这上面还装配了CategoryMapper
package com.how2java.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
public List<Category> list(){
return categoryMapper.list();
}
}
CategoryController
package com.how2java.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.how2java.pojo.Category;
import com.how2java.service.CategoryService;
// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(){
ModelAndView mav = new ModelAndView();
List<Category> cs= categoryService.list();
// 放入转发参数
mav.addObject("cs", cs);
// 放入jsp路径
mav.setViewName("listCategory");
return mav;
}
}
web.xml(在WEB-INF目录下)
<?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>
applicationContext.xml(在src目录下的,注意,这里要记得填写自己的密码)
<?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.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.how2java.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/how2java?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"/>
</bean>
</beans>
springMVC.xml(在src目录下的)
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://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.how2java.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- 视图定位 -->
<bean
class="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>
listCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
第一次运行不成功,后面发现原因是前面的密码没有填写,虽然我还特意注意了一下有没有需要填写密码的地方,但是以往需要填写密码都是以"admin"蓝色字体形式存在于代码中,这次不在引号中所以被略过。applicationContext.xml中配置了数据源,所以需要授权。
做到这里第二次运行还是不成功,查看提供的源代码发现这是因为没有将log4j-properties复制粘贴到eclipse文件夹中
完善后再次运行就得到了想要的结果。
SSM分页
1、分页类Page,用于存放分页信息,start: 开始位置,count: 每页的个数,last: 最后一页的位置,caculateLast()方法: 通过总数total和每页的个数计算出最后一页的位置
package com.how2java.util;
public class Page {
int start=0;
int count = 5;
int last = 0;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public void caculateLast(int total) {
// 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
if (0 == total % count)
last = total - count;
// 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
else
last = total - total % count;
}
}
2、修改Category.xml,增加了total sql语句
<?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="com.how2java.mapper.CategoryMapper">
<insert id="add" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="delete" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="get" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="update" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="list" resultType="Category">
select * from category_
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="total" resultType="int">
select count(*) from category_
</select>
</mapper>
3、修改CategoryMapper,增加total方法用于调用Category.xml 中total对应的sql语句,增加 list(Page page),根据分页来查询数据
package com.how2java.mapper;
import java.util.List;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public List<Category> list();
public List<Category> list(Page page);
public int total();
}
4、修改CategoryService,增加 list(Page page),根据分页来查询数据
package com.how2java.service;
import java.util.List;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
public interface CategoryService {
List<Category> list();
int total();
List<Category> list(Page page);
}
5、修改 CategoryServiceImpl,实现total()和list(Page page) 方法
package com.how2java.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
public List<Category> list(){
return categoryMapper.list();
}
@Override
public List<Category> list(Page page) {
// TODO Auto-generated method stub
return categoryMapper.list(page);
}
@Override
public int total() {
return categoryMapper.total();
};
}
6、CategoryController
package com.how2java.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.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(Page page){
ModelAndView mav = new ModelAndView();
List<Category> cs= categoryService.list(page);
int total = categoryService.total();
page.caculateLast(total);
// 放入转发参数
mav.addObject("cs", cs);
// 放入jsp路径
mav.setViewName("listCategory");
return mav;
}
7、listCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div style="width:500px;margin:0px auto;text-align:center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
<div style="text-align:center">
<a href="?start=0">首 页</a>
<a href="?start=${page.start-page.count}">上一页</a>
<a href="?start=${page.start+page.count}">下一页</a>
<a href="?start=${page.last}">末 页</a>
</div>
</div>
8、测试类MybatisTest
package com.how2java.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
@Autowired
private CategoryMapper categoryMapper;
// @Test
public void testAdd() {
for (int i = 0; i < 100; i++) {
Category category = new Category();
category.setName("new Category");
categoryMapper.add(category);
}
}
@Test
public void testTotal() {
int total = categoryMapper.total();
System.out.println(total);
}
@Test
public void testList() {
Page p = new Page();
p.setStart(2);
p.setCount(3);
List<Category> cs=categoryMapper.list(p);
for (Category c : cs) {
System.out.println(c.getName());
}
}
}
9、运行listCategory.jsp得到
SSM PageHelper
1、导入另外的jar包pagehelper-5.1.0-beta2.jar,jsqlparser-1.0.jar
2、修改applicationContext.xml,增加PageHelper配置(在原代码37行后面增加如下代码)
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
</value>
</property>
</bean>
</array>
</property>
3、CategoryService,去掉total方法和list(page)方法
package com.how2java.service;
import java.util.List;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
public interface CategoryService {
List<Category> list();
}
4、CategoryServiceImpl,也是去掉了total方法和list(page)方法
package com.how2java.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
public List<Category> list(){
return categoryMapper.list();
}
}
5、CategoryMapper
package com.how2java.mapper;
import java.util.List;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public List<Category> list();
}
6、Category.xml,去掉了total对应的sql语句,list也去掉limit
<?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="com.how2java.mapper.CategoryMapper">
<insert id="add" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="delete" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="get" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="update" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="list" resultType="Category">
select * from category_
</select>
</mapper>
7、CategoryController
package com.how2java.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(Page page){
ModelAndView mav = new ModelAndView();
PageHelper.offsetPage(page.getStart(),5);
List<Category> cs= categoryService.list();
int total = (int) new PageInfo<>(cs).getTotal();
page.caculateLast(total);
// 放入转发参数
mav.addObject("cs", cs);
// 放入jsp路径
mav.setViewName("listCategory");
return mav;
}
}
8、MybatisTest
package com.how2java.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
// @Autowired
// private CategoryMapper categoryMapper;
//
@Test
// public void testAdd() {
// for (int i = 0; i < 100; i++) {
// Category category = new Category();
// category.setName("new Category");
// categoryMapper.add(category);
// }
//
// }
//
// @Test
// public void testTotal() {
// int total = categoryMapper.total();
// System.out.println(total);
// }
//
// @Test
// public void testList() {
// Page p = new Page();
// p.setStart(2);
// p.setCount(3);
// List<Category> cs=categoryMapper.list(p);
// for (Category c : cs) {
// System.out.println(c.getName());
// }
// }
}
9、运行得到
SSM 连接池
1、修改applicationContext.xml,注释掉52-66行的原有datasource,新增Druid连接池
<?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.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.how2java.service" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="admin" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="3" />
<property name="minIdle" value="3" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<!-- <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/how2java?characterEncoding=UTF-8</value> -->
<!-- </property> -->
<!-- <property name="username"> -->
<!-- <value>root</value> -->
<!-- </property> -->
<!-- <property name="password"> -->
<!-- <value>123456</value> -->
<!-- </property> -->
<!-- </bean> -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"/>
</bean>
</beans>
2、运行可得
SSM CRUD
1、修改applicationContext.xml
<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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.how2java.service"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="3"/>
<property name="minIdle" value="3"/>
<property name="maxActive" value="20"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
</bean>
<!-- <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/how2java?characterEncoding=UTF-8</value> -->
<!-- </property> -->
<!-- <property name="username"> -->
<!-- <value>root</value> -->
<!-- </property> -->
<!-- <property name="password"> -->
<!-- <value>123456</value> -->
<!-- </property> -->
<!-- </bean> -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo"/>
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"/>
</bean>
</beans>
2、CategoryService
package com.how2java.service;
import java.util.List;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
public interface CategoryService {
List<Category> list();
int total();
List<Category> list(Page page);
void add(Category c);
void update(Category c);
void delete(Category c);
Category get(int id);
}
3、CategoryServiceImpl
package com.how2java.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
public List<Category> list(){
return categoryMapper.list();
}
@Override
public List<Category> list(Page page) {
// TODO Auto-generated method stub
return categoryMapper.list(page);
}
@Override
public int total() {
return categoryMapper.total();
}
@Override
public void add(Category c) {
categoryMapper.add(c);
}
@Override
public void update(Category c) {
categoryMapper.update(c);
}
@Override
public void delete(Category c) {
categoryMapper.delete(c.getId());
}
@Override
public Category get(int id) {
// TODO Auto-generated method stub
return categoryMapper.get(id);
};
}
4、CategoryMapper
package com.how2java.mapper;
import java.util.List;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public List<Category> list();
public List<Category> list(Page page);
public int total();
}
5、Catagory.xml
<mapper namespace="com.how2java.mapper.CategoryMapper">
<insert id="add" parameterType="Category"> insert into category_ ( name ) values (#{name}) </insert>
<delete id="delete" parameterType="Category"> delete from category_ where id= #{id} </delete>
<select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select>
<update id="update" parameterType="Category"> update category_ set name=#{name} where id=#{id} </update>
<select id="list" resultType="Category">
select * from category_ order by id desc
<if test="start!=null and count!=null"> limit #{start},#{count} </if>
</select>
<select id="total" resultType="int"> select count(*) from category_ </select>
</mapper>
6、CategoryController
package com.how2java.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.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(Page page){
ModelAndView mav = new ModelAndView();
List<Category> cs= categoryService.list(page);
int total = categoryService.total();
page.caculateLast(total);
mav.addObject("cs", cs);
mav.setViewName("listCategory");
return mav;
}
@RequestMapping("addCategory")
public ModelAndView addCategory(Category category){
categoryService.add(category);
ModelAndView mav = new ModelAndView("redirect:/listCategory");
return mav;
}
@RequestMapping("deleteCategory")
public ModelAndView deleteCategory(Category category){
categoryService.delete(category);
ModelAndView mav = new ModelAndView("redirect:/listCategory");
return mav;
}
@RequestMapping("editCategory")
public ModelAndView editCategory(Category category){
Category c= categoryService.get(category.getId());
ModelAndView mav = new ModelAndView("editCategory");
mav.addObject("c", c);
return mav;
}
@RequestMapping("updateCategory")
public ModelAndView updateCategory(Category category){
categoryService.update(category);
ModelAndView mav = new ModelAndView("redirect:/listCategory");
return mav;
}
}
7、MybatisTest
package com.how2java.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
@Autowired
private CategoryMapper categoryMapper;
// @Test
public void testAdd() {
for (int i = 0; i < 100; i++) {
Category category = new Category();
category.setName("new Category");
categoryMapper.add(category);
}
}
@Test
public void testTotal() {
int total = categoryMapper.total();
System.out.println(total);
}
@Test
public void testList() {
Page p = new Page();
p.setStart(2);
p.setCount(3);
List<Category> cs=categoryMapper.list(p);
for (Category c : cs) {
System.out.println(c.getName());
}
}
}
8、listCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div style="width:500px;margin:0px auto;text-align:center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td><a href="editCategory?id=${c.id}">编辑</a></td>
<td><a href="deleteCategory?id=${c.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<div style="text-align:center">
<a href="?start=0">首 页</a>
<a href="?start=${page.start-page.count}">上一页</a>
<a href="?start=${page.start+page.count}">下一页</a>
<a href="?start=${page.last}">末 页</a>
</div>
<div style="text-align:center;margin-top:40px">
<form method="post" action="addCategory">
分类名称: <input name="name" value="" type="text"> <br><br>
<input type="submit" value="增加分类">
</form>
</div>
</div>
9、eidtCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div style="width:500px;margin:0px auto;text-align:center">
<div style="text-align:center;margin-top:40px">
<form method="post" action="updateCategory">
分类名称: <input name="name" value="${c.name}" type="text"> <br><br>
<input type="hidden" value="${c.id}" name="id">
<input type="submit" value="增加分类">
</form>
</div>
</div>
10、但是就是运行不出来。。。还在找原因