博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring+springmvc+mybatis+logback 框架搭建
阅读量:6437 次
发布时间:2019-06-23

本文共 10788 字,大约阅读时间需要 35 分钟。

hot3.png

注:本文仅仅属于个人记录文档。

一、项目的总体结构、以及所需要的jar包

1.项目结构(注:此处的rsrc/resources为资源目录,新建时New -> source folder)

151839_FBMl_2490316.png

2.所需要的jar包

152603_VHMw_2490316.png

二、配置文件源代码

a. web.xml

portals
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
spring
/
/WEB-INF/login.jsp

b.data.properties

driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/userjdbc.username=jdbc.password=webServer=http://localhost:8080/portals

c.applicationContext.xml

d.spring-mvc.xml

e.mybatis-config.xml

f.logback.xml

true
%d{yyyy-MM-dd HH:mm:ss}-%-5level %logger{0} - %msg%n
UTF-8
D:/portals/logs/%d{yyyy-MM-dd}-%i.log
365
50MB
%d{yyyy-MM-dd HH:mm:ss} - [ %-5level ] [ %logger{0} : %line ] - %msg%n

三、java文件代码

a.User.java

package com.study.pojo;import java.io.Serializable;public class User implements Serializable {	private static final long serialVersionUID = 1L;	private int id;	private String name;	private String sex;	private int age;	public User() {	}	public User(int id, String name, String sex, int age) {		super();		this.id = id;		this.name = name;		this.sex = sex;		this.age = age;	}	public int getId() {		return id;	}	public void setId(int id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getSex() {		return sex;	}	public void setSex(String sex) {		this.sex = sex;	}	public int getAge() {		return age;	}	public void setAge(int age) {		this.age = age;	}	@Override	public String toString() {		return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";	}}

b.UserController.java

package com.study.controller;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.study.pojo.User;import com.study.service.UserService;import com.study.util.JsonResult;@Controllerpublic class UserController {	@Autowired	private UserService userService;		@Value("#{settings.driver}")	private String name;		@RequestMapping("login")	@ResponseBody  //不可缺少该注解	public JsonResult login(User user,HttpServletRequest req) {		System.out.println("================"+user);				JsonResult jsonResult = new JsonResult();		jsonResult.setFlag(true);		jsonResult.setObject(user);		jsonResult.setMessage("success");		return jsonResult;	}		@RequestMapping("user")	public String userList(Model model,HttpServletRequest req) {		List
userList = userService.getUserList(); /*for(int i=100; i<105; i++){ userList.add(new User(i,"zhangsan","男",i-80)); }*/ System.out.println(userList.toString()); model.addAttribute("userList", userList); return "user"; } @RequestMapping("add") public String addUser(Model model,HttpServletRequest req) { User user = new User(57,"jack","男",18); userService.addUser(user); /*for(int i=100; i<105; i++){ userList.add(new User(i,"zhangsan","男",i-80)); }*/ System.out.println(user); return ""; } @RequestMapping("test") public String test(Model model,HttpServletRequest req) { System.out.println("------------------test--------------------"); List
userList = userService.getUserList(); /*for(int i=100; i<105; i++){ userList.add(new User(i,"zhangsan","男",i-80)); }*/ System.out.println(userList.toString()); model.addAttribute("userList", userList); return "test"; } @RequestMapping("test2") public String test2(Model model,HttpServletRequest req) { System.out.println(name +"------------------name--------------------"); return "test"; }}

c.UserServiceImpl.java      (实现UserService接口)

package com.study.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.study.dao.UserDao;import com.study.pojo.User;import com.study.service.UserService;@Servicepublic class UserServiceImpl implements UserService {	@Autowired	private UserDao userDao;	@Override	public List
getUserList() { List
userList= userDao.getUserList(); return userList; } //测试事务 @Override public int addUser(User user) { System.out.println("----111----test-tx----------------"); int j = userDao.deleteUser(58); System.out.println("---222-----test-tx----------------"); int i = userDao.addUser(user); System.out.println("---333-----test-tx----------------"); return 5; } @Override public int deleteUser(int id) { int i = userDao.deleteUser(id); return i; } @Override public int updateUser(int id) { int i = userDao.updateUser(id); return i; }}

d.UserDao.java

package com.study.dao;import java.util.List;import org.springframework.stereotype.Repository;import com.study.pojo.User;@Repositorypublic interface UserDao {	public List
getUserList(); public int addUser(User user); public int deleteUser(int id); public int updateUser(int id); }

e.mybatis-sql-user.xml

f.WebConfig.java

package com.study.util;import javax.servlet.ServletContext;import org.springframework.web.context.ServletContextAware;public class WebConfig implements ServletContextAware {	private String beanName;	//@Value("#{settings.webServer}")	private String webServer;		@Override	public void setServletContext(ServletContext servletContext) {		servletContext.setAttribute(beanName, webServer);	}	public String getBeanName() {		return beanName;	}	public void setBeanName(String beanName) {		this.beanName = beanName;	}	public String getWebServer() {		return webServer;	}	public void setWebServer(String webServer) {		this.webServer = webServer;	}	}

g.JsonResult.java

package com.study.util;public class JsonResult {	private boolean flag;	private Object object;	private String message;		public boolean isFlag() {		return flag;	}	public void setFlag(boolean flag) {		this.flag = flag;	}	public Object getObject() {		return object;	}	public void setObject(Object object) {		this.object = object;	}	public String getMessage() {		return message;	}	public void setMessage(String message) {		this.message = message;	}	}

四、jsp文件

a. user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
user list

${webServer}

${user.id}

${user.name}

${user.sex}

${user.age}

b.test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>	
my first bootstrap page
悬停表格布局
id 姓名 性别 年龄
${user.id} ${user.name} ${user.sex} ${user.age}
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo.
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo.

c login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here

跳转

姓名:
性别:
年龄:

 

转载于:https://my.oschina.net/u/2490316/blog/781799

你可能感兴趣的文章
优秀员工与普通员工
查看>>
CCNP学习笔记15-RSTP
查看>>
DELL服务器iDRAC相关设置
查看>>
JVM学习笔记(一)------基本结构
查看>>
$@等特定shell变量的含义
查看>>
我的友情链接
查看>>
(超详细版)Linux下Hadoop2.7.1集群环境的搭建(3台为例)
查看>>
策略模式、上下文与内部类的思考
查看>>
关于getCurrentUrl的获取问题
查看>>
浅说动态生成Class实现MVC
查看>>
永乐XIANDAI 08
查看>>
基于 HTML5 的 WebGL 3D 档案馆可视化管理系统
查看>>
ActiveMQ点对点消息通信demo
查看>>
pybot --help
查看>>
AntDB上使用表空间
查看>>
【学习笔记11】动态方法调用和使用通配符定义action
查看>>
java中的final关键字
查看>>
微信小程序------MD5加密(支持中文和不支持中文)和网络请求(get和post)
查看>>
android -------- java虚拟机和Dalvik虚拟机
查看>>
AJAX——基于请求加载数据
查看>>