最近研究java web开发,首先需要配置好开发环境,查阅网上各位大神的文章后搭建完成,记录下来作为备忘。
Maven下载地址:http://maven.apache.org/download.cgi
在eclipse window选项中选择Preference可以配置Maven。
具体可以参考如下步骤。
在Installations新建一项配置maven的安装目录:
国内Maven repository下载比较慢,可以配置conf\settings.xml新增repository镜像:
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
central
repo1
repo1 maven
http://repo1.maven.org/maven2/
central
在eclipse中新建Project ->Maven->Maven Project:
选择Workspace,这里我使用默认的Workspace:
选择maven-archetype-webapp:
定义Group Id和Artifact Id,Package为Group Id+Artifact Id:
右键点击项目选择Properties,首先修改Java Build Path:
选中左边Project Facet点击Convert faceted from...
如图所示依次选中各项:
选中左边新出现的Deployment Assembly,配置和下图类似:
选中左边Web Project Setting,确保Context root与项目名一致:
maven web项目建立完成,结构如下图:
1、pom.xml配置项目依赖包
具体配置如下,可以作为参考,其中配置了maven 的 mybatis generator 插件后面可以用来自动生成DAO Entity 和 数据库映射xml。
4.0.0
cn.hanwest.webapp
ssmweb
war
0.0.1-SNAPSHOT
ssmweb Maven Webapp
http://maven.apache.org
4.0.2.RELEASE
3.2.6
1.7.7
1.2.17
1.3.5
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
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
${log4j.version}
com.alibaba
fastjson
1.1.41
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
org.mybatis.generator
mybatis-generator-core
${mybatis.generator.version}
ssmweb
org.mybatis.generator
mybatis-generator-maven-plugin
${mybatis.generator.version}
src/main/resources/generatorConfig.xml
true
true
mysql
mysql-connector-java
5.1.30
org.mybatis.generator
mybatis-generator-core
${mybatis.generator.version}
2、配置mybatis
在src/main/resources中新建jdbc配置文件jdbc.properties (注意配置文件转为UTF-8并且行尾不能有空格)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/testssmdb
username=root
password=
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
在src/main/resources中新建mybatis配置文件spring-mybatis.xml(注意包名改为当前项目的包名)
3、使用maven mybatis generator自动生成DAO Entity 和 mapper xml
在mysql中建表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`nickname` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`phone` varchar(127) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
在src/main/resources中新建配置文件generatorConfig.xml(注意包名改为当前项目的包名以及修改xml中的jdbc配置)
建立和配置文件中响应的包名,结构与下图类似
右键点击项目选择Run As Maven Build...(注意有...)
在出现的窗口中Goals:输入mybatis-generator:generate -X 点击Run执行
执行完成后可以看到Console中有类似的log并且在相应的包中会生成java文件
自动生成成功。
4、配置spring mvc
分别建立controller、servcie类
UserController.java
package cn.hanwest.webapp.ssmweb.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.hanwest.webapp.ssmweb.entity.User;
import cn.hanwest.webapp.ssmweb.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/profile")
public String profile(HttpServletRequest request, Model model) {
String idStr = request.getParameter("id");
if(null != idStr && "".equals(idStr.trim())){
int userId = Integer.valueOf(idStr);
User user = this.userService.getUserById(userId);
model.addAttribute("user", user);
return "User/profile";
} else {
return "index";
}
}
@RequestMapping("/register")
public String register(HttpServletRequest request, Model model) {
return "User/register";
}
@RequestMapping(value="/register", method=RequestMethod.POST)
public String doRegister(HttpServletRequest request, Model model) {
String username = request.getParameter("username");
String nickname = request.getParameter("nickname");
String password = request.getParameter("password");
String phone = request.getParameter("phone");
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setPhone(phone);
user.setNickname(nickname);
int id = userService.addUser(user);
return String.format("User/profile?id=%d", id);
}
}
package cn.hanwest.webapp.ssmweb.service;
import cn.hanwest.webapp.ssmweb.entity.User;
public interface UserService {
public User getUserById(int userId);
public int addUser(User user);
}
package cn.hanwest.webapp.ssmweb.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.hanwest.webapp.ssmweb.dao.UserMapper;
import cn.hanwest.webapp.ssmweb.entity.User;
import cn.hanwest.webapp.ssmweb.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userDao;
@Override
public User getUserById(int userId) {
// TODO Auto-generated method stub
return userDao.selectByPrimaryKey(userId);
}
@Override
public int addUser(User user) {
// TODO Auto-generated method stub
return userDao.insertSelective(user);
}
}
在src/main/webapp/WEB-INF中建立视图文件
profile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
测试
${user.userName}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
注册
在src/main/resources中新建配置文件spring-mvc.xml(注意包名改为当前项目的包名)
text/html;charset=UTF-8
修改src/main/webapp/WEB-INF中的web.xml
Archetype Created Web Application
contextConfigLocation
classpath:spring-mybatis.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
SpringMVC
/
/index.jsp
右键点击项目Run As -》Run on Server输入user/register访问成功说明框架配置基本完成,如有问题请排查。