application-mvc.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:oscache="http://www.springmodules.org/schema/oscache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd"> <context:annotation-config/> <!-- 扫描包 --> <context:component-scan base-package="com.org"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 匹配jsp文件下面的所有.jsp的页面 --> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />--> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <property name="maxActive" value="500"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <!-- 定义Ibatis配置 org.springframework.orm.ibatis.SqlMapClientFactoryBean --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:ibatis-Base.xml</value> </property> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- 配置sqlMapClientTemplate模板 --> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> <!-- 配置 transactionManager事物管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- Spring AOP config配置切点 --> <aop:config> <aop:pointcut expression="execution(* com.org.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService"/> </aop:config> <!-- 配置那个类那个方法用到事务处理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
web.xml
<?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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- #####################################配置处理乱码##################################### --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <!-- 默认的配置 <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> <param-value>/WEB-INF/application-servlet.xml</param-value> --> <param-value>classpath:application-*.xml</param-value> </context-param> <!-- #####################################Spring MVC配置################################# application-servlet.xml,规定:xxx-servlet.xml--> <servlet> <servlet-name>application</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>application</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
model-user.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="userSqlMap"> <typeAlias alias="user" type="com.org.model.User" /> <!-- 条件查询 --> <select id="getUserList" resultClass="user"> select * from user </select> </sqlMap>
service
package com.org.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.org.dao.IUserDao;
import com.org.model.User;
import com.org.service.IUserService;
@Service
public class UserServiceImpl implements IUserService{
/**
* 注入
*/
@Resource
private IUserDao userDao;
public List<User> getUserList() {
return userDao.getUserList();
}
}
Dao
package com.org.dao.impl;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.org.IbatisBaseDao;
import com.org.dao.IUserDao;
import com.org.model.User;
@Repository
public class UserDaoImpl extends IbatisBaseDao implements IUserDao{
public List<User> getUserList() {
try {
//return sqlMapClient.queryForList("getUserList");
return getSqlMapClientTemplate().queryForList("getUserList");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
IbatisBaseDao
package com.org;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;
/**
* @author:liangjilong
*/
public class IbatisBaseDao extends SqlMapClientDaoSupport{
/**
* spring注入
*/
@Resource(name = "sqlMapClient")
private SqlMapClient sqlMapClient;
@PostConstruct
public void initSqlMapClient() {
super.setSqlMapClient(sqlMapClient);
}
}
UserController
package com.org.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.org.model.User;
import com.org.service.IUserService;
/**
*@Author:liangjilong
*@Date:2014-2-25
*@Version:1.0
*@Description:
*/
@Controller
public class UserController {
@Resource
private IUserService userService;
/***
* 方法一请求使用String
*
* 请求@RequestMapping匹配的URL
* request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/userList1.do")
public String userList1(HttpServletRequest request,HttpServletResponse response) throws Exception {
List<User> lists=userService.getUserList();
if(lists!=null){
request.setAttribute("userList",lists);
}
//userList指跳转到userList.jsp页面
return "userList";
}
/**
* 方法二请求使用ModelAndView
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/userList2.do")
public ModelAndView userList2(HttpServletRequest request,HttpServletResponse response) throws Exception {
List<User> lists=userService.getUserList();
if(lists!=null){
request.setAttribute("userList",lists);
}
//userList指跳转到userList.jsp页面
return new ModelAndView("userList");
}
}
SQLScript
/*
Navicat MySQL Data Transfer
Source Server : MySQL
Source Server Version : 50528
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001
Date: 2014-03-30 12:52:38
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin', 'admin');
Entity
package com.org.model;
import com.org.BaseEntity;
public class User extends BaseEntity {
private int id;
private String password;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}