使用Intellij idea 搭建基本的maven空项目 new->project
最后Finish,Maven项目搭建完成
构建项目基本骨架
1,在java下新建com
2,在com下新建springmvc
3,在springmvc下新建controller、dao、mapping、pojo、service
如下图:
目录解释如下
controller:http(GET和POST等)请求的入口,调用Service,想客户端返回数据
dao:接口,定义了数据表映射成的对象,具有的操作
mapping:具体是sql语句,以xml格式存在,即mybatis的语法文件
service:controller调用的服务,service调用dao里面定义的数据库行为方法,来执行对应mapping里的mybatis,从而完成与数据库完整的交互。
配置你的maven
在pom.xml文件中添加如下内容
4.0.0
com
ssm
war
1.0-SNAPSHOT
Maven Webapp
http://maven.apache.org
UTF-8
4.1.4.RELEASE
3.2.8
1.7.7
1.2.17
2.5.0
junit
junit
3.8.1
test
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
org.springframework
spring-test
${spring.version}
test
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-jdbc
${spring.version}
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
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
com.fasterxml.jackson.core
jackson-databind
2.1.0
com.fasterxml.jackson.core
jackson-annotations
2.1.0
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
junit
junit
4.11
test
org.springframework
spring-test
4.1.7.RELEASE
junit
junit
RELEASE
org.jsoup
jsoup
1.8.1
net.sourceforge.htmlunit
htmlunit
2.15
org.seleniumhq.selenium
selenium-java
2.33.0
us.codecraft
webmagic-core
0.6.1
us.codecraft
webmagic-extension
0.6.1
com
src/main/java
**/*.xml
true
在resources下新建个配置文件jdbc.properties,内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=root
定义初始连接数
initialSize=0
定义最大连接数
maxActive=20
定义最大空闲
maxIdle=20
定义最小空闲
minIdle=1
定义最长等待时间
maxWait=60000
请确保你的mysql服务是开启的,且存在数据库test
text/html;charset=UTF-8
日志模块的配置:
定义LOG输出级别
log4j.rootLogger=INFO,Console,File
定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
指定输出目录
log4j.appender.File.File = logs/ssm.log
定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n
Archetype Created Web Application
contextConfigLocation
classpath:spring-mybatis.xml
log4jRefreshInterval
60000
org.springframework.web.util.Log4jConfigListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
mvc-dispatcher
/
15
index.jsp
把webapp添加入项目工程
添加artifacts
配置tomcat
package com.springmvc.controller;
import com.springmvc.pojo.Admin;
import com.springmvc.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@Controller
public class main {
@Autowired
private AdminService adminService;
/**
* 获取所有信息
* @param request
* @param model
* @return
*/
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request, Model model){
List admin=adminService.findAll();
System.out.println(admin);
model.addAttribute("list",admin);
request.setAttribute("list",admin);
return "allUser";
}
/**
* 添加信息
* @param admin
* @param model
* @return
*/
@RequestMapping("/addUser")
public String addUser(Admin admin, Model model){
adminService.save(admin);
return "redirect:getAllUser";
}
/**
* 修改信息
* @param admin
* @param request
* @param model
* @return
*/
@RequestMapping("/updateUser")
public String updateUser(Admin admin,HttpServletRequest request,Model model){
if(adminService.update(admin)){
admin=adminService.findById(admin.getId());
request.setAttribute("admin",admin);
model.addAttribute("admin",admin);
return "redirect:getAllUser";
}else{
return "error";
}
}
/**
* 根据id查询
* @param id
* @param request
* @param model
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id,HttpServletRequest request,Model model){
request.setAttribute("admin",adminService.findById(id));
model.addAttribute("admin",adminService.findById(id));
return "editUser";
}
/**
* 删除用户
* @param id
*/
@RequestMapping("/delUser")
public String delUser(int id){
adminService.delete(id);
return "redirect:getAllUser";
}
/**
* 注册账号
* @param admin
* @param model
* @return
*/
@RequestMapping("/register")
public String register(Admin admin, Model model){
adminService.save(admin);
return "login";
}
/**
* 登录账号
* @return
*/
@RequestMapping("/login")
public String login(Admin admin,Model model){
Admin ad=adminService.login(admin);
if(ad!=null){
return "redirect:getAllUser";
}else{
return "error";
}
}
}
service层代码
package com.springmvc.service;
import com.springmvc.dao.AdminMapper;
import com.springmvc.pojo.Admin;
import com.sun.jna.platform.win32.Netapi32Util;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("AdminService")
public class AdminService {
@Resource
private AdminMapper adminMapper;
/**
* 添加数据
*/
public void save(Admin admin) {
adminMapper.save(admin);
}
/**
* 根据id修改
* @param admin
* @return
*/
public boolean update(Admin admin) {
return adminMapper.update(admin);
}
/**
* 根据id删除
* @param id
* @return
*/
public boolean delete(int id) {
return adminMapper.delete(id);
}
/**
* 根据id查询
* @param id
* @return
*/
public Admin findById(int id) {
Admin admin=adminMapper.findById(id);
return admin;
}
/**
* 查询所有
* @return
*/
public List findAll() {
List findallList=adminMapper.findAll();
return findallList;
}
/**
* 登录账号
*/
public Admin login(Admin admin){
return adminMapper.login(admin);
}
}
dao层代码
package com.springmvc.dao;
import com.springmvc.pojo.Admin;
import java.util.List;
public interface AdminMapper {
void save(Admin admin);
boolean update(Admin admin);
boolean delete(int id);
Admin findById(int id);
List findAll();
Admin login(Admin admin);
}
mapping层代码
insert into admin(name,password) values(#{name},#{password})
update admin set name=#{name},password=#{password} where id=#{id}
delete from admin where id=#{id}
pojo层代码
package com.springmvc.pojo;
public class Admin {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
@Override
public String toString() {
return "Admin{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
源码下载https://download.csdn.net/download/sinat_37001576/10357678