创建maven的web项目
next后面自己写
配置pom.xml
UTF-8
junit
junit
4.10
test
org.springframework
spring-core
4.1.0.RELEASE
org.springframework
spring-web
4.1.0.RELEASE
org.springframework
spring-oxm
4.1.0.RELEASE
org.springframework
spring-tx
4.1.0.RELEASE
org.springframework
spring-jdbc
4.1.0.RELEASE
org.springframework
spring-webmvc
4.1.0.RELEASE
org.springframework
spring-aop
4.1.0.RELEASE
org.springframework
spring-context-support
4.1.0.RELEASE
org.springframework
spring-test
4.1.0.RELEASE
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
1.2.17
com.alibaba
fastjson
1.1.41
org.slf4j
slf4j-api
1.7.7
org.slf4j
slf4j-log4j12
1.7.12
com.fasterxml.jackson.core
jackson-core
2.5.3
com.fasterxml.jackson.core
jackson-databind
2.5.3
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
配置web.xml
Archetype Created Web Application
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:spring-servlet.xml
spring-mvc
/
配置spring-servlet.xml
在resource目录下创建spring-servlet.xml
classpath:*.properties
text/html;charset=UTF-8
text/html;charset=UTF-8
application/json;charset=UTF-8
数据库连接配置
- 配置jdbc.properties
#jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=1234
jdbc.url=jdbc:mysql://127.0.0.1:3306/mydatabase?useUnicode=true&characterEncoding=utf-8
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000
- 配置spring-ds.xml
classpath:mybatis/mapper/**/*.xml
mybatis配置
在resource目录下创建mybatis目录。然后创建mybatis-config.xml
在resource/mybatis目录下创建mapper目录, mybatis相关mapper文件放在resource/mybatis/mapper目录下。
这里推荐使用mybatis_generator插件生成数据库对应的mybatis的mapper文件和实体类
简单的generator配置文件
建议:工具生成的文件最好不用动,表字段如果有更新直接覆盖文件,如果需要手写sql,最好把自己新增的sql创建一个新文件 xxxxMapper_extend.xml,然后在里面写,对应的DAO也用一个新的dao继承原来工具创建的DAO。
这里我创建一个user表,用工具生成对应的文件,文件结构如下,UserMapper即DAO
然后配置log4j
log4j.rootLogger = DEBUG, Console
log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.Target = System.out
log4j.appender.A.layout = org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern = %p %t %c - %m%n
log4j.appender.B = org.apache.log4j.DailyRollingFileAppender
log4j.appender.B.File = logs/log.log
log4j.appender.B.Append = true
log4j.appender.B.Threshold = DEBUG
log4j.appender.B.layout = org.apache.log4j.PatternLayout
log4j.appender.B.layout.ConversionPattern = %p %t %c - %m%n
log4j.appender.C = org.apache.log4j.DailyRollingFileAppender
log4j.appender.C.File = logs/error.log
log4j.appender.C.Append = true
log4j.appender.C.Threshold = ERROR
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern =%p %t %c - %m%n
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO
创建一个简单的demo
1.创建service接口&实现类
public interface UserService {
Object doAddUser(User user);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public Object doAddUser(User user) {
userMapper.insertSelective(user);
return user;
}
}
2.创建controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/addUser")
public Object addUser(String name){
User user = new User();
user.setName(name);
user = (User) userService.doAddUser(user);
return user;
}
}
3.修改默认的index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
test
用户名:
这里我加入了jquery.js ,你们自己下一个加进去。
配置tomcat
效果
项目已上传至github:https://github.com/StoneKings/Base_web_SM.git