一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么。
暂时没有整合缓存,druid也没有做ip地址的过滤。Spring的AOP简单配置了下,也还没具体弄,不知道能不能用,log也不知道能不能用,`(*∩_∩*)′哈哈,有点不负责任......
2014-08-12 23:45补:
=================开始=================1、增加quartz :http://www.cnblogs.com/acehalo/p/3902731.html
2、mybatis添加ehcache缓存:http://www.cnblogs.com/acehalo/p/3902883.html
3、spring的aop: http://www.cnblogs.com/acehalo/p/3905718.html
=================结束================
直接上代码:
使用的eclipse和eclipse自带的maven,参考了网上的资料,有些代码是拷贝的,不过都自己测试过了。嗯,可以跑起来...
先上项目结构:
新建maven项目,选择web,然后配置pom:
4.0.0
ACE
Test
war
0.0.1-SNAPSHOT
Test Maven Webapp
http://maven.apache.org
4.0.0.M2
1.6.1
junit
junit
3.8.1
test
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.1
provided
javax.servlet
jstl
1.2
mysql
mysql-connector-java
5.1.26
org.springframework
spring-aop
${org.springframework.version}
org.springframework
spring-aspects
${org.springframework.version}
org.springframework
spring-beans
${org.springframework.version}
org.springframework
spring-context
${org.springframework.version}
commons-logging
commons-logging
org.springframework
spring-context-support
${org.springframework.version}
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-expression
${org.springframework.version}
org.springframework
spring-instrument
${org.springframework.version}
org.springframework
spring-instrument-tomcat
${org.springframework.version}
org.springframework
spring-jdbc
${org.springframework.version}
org.springframework
spring-jms
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-oxm
${org.springframework.version}
org.springframework
spring-test
${org.springframework.version}
org.springframework
spring-tx
${org.springframework.version}
org.springframework
spring-web
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.springframework
spring-webmvc-portlet
${org.springframework.version}
org.springframework
spring-websocket
${org.springframework.version}
org.mybatis
mybatis
3.2.2
org.mybatis
mybatis-spring
1.2.0
org.slf4j
slf4j-api
${org.slf4j.version}
org.slf4j
jcl-over-slf4j
${org.slf4j.version}
runtime
log4j
log4j
1.2.16
runtime
com.alibaba
druid
1.0.7
org.jboss.repository.release
JBoss Maven Release Repository
https://repository.jboss.org/nexus/content/repositories/releases
false
org.springsource.maven.snapshot
SpringSource Maven Central-compatible Snapshot Repository
http://maven.springframework.org/snapshot
daily
spring-milestone
Spring Maven MILESTONE Repository
http://maven.springframework.org/milestone
standard repo
http://repo1.maven.org/maven2
mirror repo
http://mirrors.ibiblio.org/pub/mirrors/maven2
com.springsource.repository.bundles.release
SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases
http://repository.springsource.com/maven/bundles/release
com.springsource.repository.bundles.external
SpringSource Enterprise Bundle Repository - External Bundle Releases
http://repository.springsource.com/maven/bundles/external
sqlfire-release
http://dist.gemstone.com/maven/release
Test
web.xml:
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5">
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
org.springframework.web.context.request.RequestContextListener
Test
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:applicationContext.xml
1
Test
*.do
404
/error/404.jsp
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
DruidStatView
/druid/*
applicationContext.xml:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
classpath:log4j.xml
spring-mvc.xml:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
spring-aop.xml:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
">
spring-database.xml:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
SELECT 1
com.hi.test.service.*
mybatis-config.xml:
/p>
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
log4j.xml:
commons-logging.properties:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
IndexController:
packagecom.hi.test.controller;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importcom.hi.test.bean.User;importcom.hi.test.mapper.UserMapper;importcom.hi.test.service.TxTestService;
@Controllerpublic classIndexController {
@Autowired
UserMapper userMapper;
@Autowired
TxTestService txTestService;
@RequestMapping(value= "/index")publicString index(HttpServletRequest request,Map model) {//String name=request.getParameter("name");//model.put("name", name);
User u=newUser();
u.setUsername("admin");
u.setPassword("admin");
User user=userMapper.selectUser(u);
model.put("user", user);return "hi";
}
@RequestMapping(value= "/text")
@ResponseBodypublicString text(HttpServletRequest request) {return "{\"what\":\"hi\"}";
}
@RequestMapping(value= "/indexList")publicString indexList(HttpServletRequest request,Map model) {
List userList=userMapper.selectUserLsit();
model.put("userList", userList);return "hiList";
}
@RequestMapping(value= "/TxTest")
@ResponseBodypublicString test(HttpServletRequest request) {
txTestService.insert();return "";
}
}
User:
packagecom.hi.test.bean;public classUser {private intid;privateString username;privateString password;publicUser(){
}public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString toString(){return "User [id = "+ id +" , username = "+ username +"]" + " , password = "+password;
}
}
UserMapper:
packagecom.hi.test.mapper;importjava.util.List;importorg.springframework.stereotype.Repository;importcom.hi.test.bean.User;
@Repositorypublic interfaceUserMapper {publicUser selectUser(User user);public voidinsertUser(User user);public voidupdateUser(User user);public void deleteUser(intuserId);publicList selectUserLsit();
}
UserMapper.xml:
/p>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT * FROM user WHERE username=#{username} AND password=#{password}
INSERT INTO user (id,username,password) VALUES (#{id},#{username},#{password})
UPDATE user SET password=#{password} WHERE id=#{id}
DELETE FROM user WHERE id=#{userId}
SELECT * FROM user
TxTestService:
packagecom.hi.test.service;importjava.util.UUID;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importcom.hi.test.bean.User;importcom.hi.test.mapper.UserMapper;
@Transactional
@Servicepublic classTxTestService {
@Autowired
UserMapper userMapper;public voidinsert() {for (int i = 0; i < 100; i++) {
User u= newUser();
u.setUsername(UUID.randomUUID().toString());
u.setPassword(UUID.randomUUID().toString());
userMapper.insertUser(u);
}throw newRuntimeException();
}
}
sql文件,navicat导出的:
/*Navicat MySQL Data Transfer
Source Server : MySQL
Source Server Version : 50704
Source Host : localhost:3306
Source Database : mydb
Target Server Type : MYSQL
Target Server Version : 50704
File Encoding : 65001
Date: 2014-08-09 23:52:32*/
SET FOREIGN_KEY_CHECKS=0;--------------------------------Table structure for user------------------------------
DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (
`id`int(11) NOT NULLAUTO_INCREMENT,
`username`varchar(255) NOT NULL,
`password`varchar(255) NOT NULL,PRIMARY KEY(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=902 DEFAULT CHARSET=utf8;--------------------------------Records of user------------------------------
INSERT INTO `user` VALUES ('1', 'admin', 'admin');
index.jsp:
Insert title herehi.jsp:
Insert title herehiList.jsp:
Insert title here${user.username}----${user.password}
======================================================================================
OK,代码贴完了,项目上右键,debug on server应该就能跑起来了。
现在简单讲讲,配置文件里面有注释就不说了:
一:在配置文件添加事务的时候遇到tx标签没用,在xsi:schemaLocation这里添加两行
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
就可以了。
二:还遇到EL表达式不能用,然后
web.xml修改:
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"
version="2.5">
jsp增加:
三:还有c:forEach这种标签不能用
在JSP上增加
就可以了
四:spring事务这里
数据库是mysql ,然后表引擎是InnoDB
用navicat在design table---option--engine可以看到
然后将表的引擎换成MyISAM,保存
说明:Spring的这个声明事务能使用,MyISAM不支持事务。
五:Controller里的四个方法
第一个方法是一般的测试。
第二个方法是想测试返回json的,主要是@ResponseBody标签。
第三个方法测试LIST。
第四个是测试事务的。