Spring+SpringMVC+Mybatis+PostgreSQL+Bootstrap入门整合
数据库:PostgreSQL 10.x版本
可视化工具:pgAdmin4
本博客主要记录小帅丶自行搭建的一个简单框架,用于日后学习及日后想要利用该平台来完成部分简单重复的工作。工具是MyEclipse 2015 CI
<spring.version>4.3.14.RELEASEspring.version>
<commons-logging.version>1.1.3commons-logging.version>
<servlet.version>3.0.1servlet.version>
<jsp-api.version>2.2jsp-api.version>
<jstl.version>1.2jstl.version>
<standard.version>1.1.2standard.version>
<junit.version>3.8.1junit.version>
<jdk.version>1.8jdk.version>
<maven.compiler.plugin.version>2.3.2maven.compiler.plugin.version>
<mybatis.version>3.4.4mybatis.version>
<mybatis.spring.version>1.3.1mybatis.spring.version>
<druid.version>1.0.20druid.version>
不完全是所有的jar版本。具体可以看pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.bjjuli.payplatformgroupId>
<artifactId>jlpaypartifactId>
<packaging>warpackaging>
<version>0.0.1version>
<name>jlpaypname>
<url>http://maven.apache.orgurl>
<properties>
<spring.version>4.3.14.RELEASEspring.version>
<commons-logging.version>1.1.3commons-logging.version>
<servlet.version>3.0.1servlet.version>
<jsp-api.version>2.2jsp-api.version>
<jstl.version>1.2jstl.version>
<standard.version>1.1.2standard.version>
<junit.version>3.8.1junit.version>
<jdk.version>1.8jdk.version>
<maven.compiler.plugin.version>2.3.2maven.compiler.plugin.version>
<mybatis.version>3.4.4mybatis.version>
<mybatis.spring.version>1.3.1mybatis.spring.version>
<druid.version>1.0.20druid.version>
properties>
<dependencies>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.35version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.36version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>${mybatis.spring.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.8.5version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.8.5version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.8.5version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.2.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>${servlet.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>${jsp-api.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>${jstl.version}version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>${standard.version}version>
dependency>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<version>42.2.1.jre7version>
dependency>
dependencies>
<build>
<finalName>jlpaypfinalName>
build>
project>
数据库配置内容,驱动用户名密码等
#数据库驱动postgresql
driver=org.postgresql.Driver
url=jdbc:postgresql://127.0.0.1:5432/test
username=postgres
password=1234
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
项目注解扫描配置等
xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="com.juli.jlpayp.controller" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
mvc:message-converters>
mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
bean>
<mvc:default-servlet-handler />
beans>
项目注解扫描配置
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.juli.jlpayp" />
beans>
数据库连接池等相关ORM配置
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.juli.jlpayp" />
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:payplat-jdbc.properties" />
bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${initialSize}">property>
<property name="maxActive" value="${maxActive}">property>
<property name="minIdle" value="${minIdle}">property>
<property name="maxWait" value="${maxWait}">property>
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/juli/jlpayp/mapping/*/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.juli.jlpayp.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
beans>
针对于项目的启动和一些拦截配置
xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>test-springdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xmlparam-value>
context-param>
<context-param>
<param-name>log4jConfigLocationparam-name>
<param-value>classpath:payplat-log4j.propertiesparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xml,classpath:spring-mybatis.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
页面请求处理层
package com.juli.jlpayp.controller;
import javax.servlet.http.HttpSession;
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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.fastjson.JSONObject;
import com.juli.jlpayp.pojo.user.User;
import com.juli.jlpayp.service.user.UserService;
@Controller
@RequestMapping(value="/user")
public class UserController {
@Autowired
UserService userService;
//登录页面跳转
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(){
return "login";
}
//登录请求处理
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(@RequestParam("userName")String userName,
@RequestParam("passWord")String passWord,
HttpSession session,Model model){
User user = userService.retrieve(userName, passWord);
System.out.println(JSONObject.toJSONString(user));
if(user!=null){
return "user/userList";
}else{
return "login";
}
}
}
业务实现接口类
package com.juli.jlpayp.service.user;
import com.juli.jlpayp.pojo.user.User;
public interface UserService {
public User retrieve(String userName,String passWord);
}
业务实现类
package com.juli.jlpayp.service.user.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.juli.jlpayp.dao.user.UserDao;
import com.juli.jlpayp.pojo.user.User;
import com.juli.jlpayp.service.user.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserDao userMapper;
public User retrieve(String userName, String passWord) {
User users = userMapper.retrieve(userName, passWord);
return users;
}
}
业务实现DAO层
package com.juli.jlpayp.dao.user;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.juli.jlpayp.pojo.user.User;
@Mapper
public interface UserDao {
public User retrieve(@Param("userName")String userName,@Param("passWord")String passWord);
}
具体的sql执行
xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.juli.jlpayp.dao.user.UserDao">
<select id="retrieve" resultType="com.juli.jlpayp.pojo.user.User">
select
"userId","userName","userPassword"
from "excel_user" where "userName" = #{userName} and "userPassword"=#{passWord}
select>
<select id="list" resultType="com.juli.jlpayp.pojo.user.User">
select
`userId`,`userName`,`userPassword`
from excel_user
<choose>
<when test="sort != null and sort.trim() != ''">
order by ${sort} ${order}
when>
<otherwise>
order by userId desc
otherwise>
choose>
<if test="offset != null and limit != null">
limit #{offset}, #{limit}
if>
select>
<select id="count" resultType="int">
select count(*) from excel_user
select>
mapper>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>登录title>
<link rel="stylesheet" href="<%=basePath%>bootstrap/css/bootstrap.min.css">
head>
<body>
<div class="container">
<h1>登录页面h1>
<hr />
<form action="<%=basePath%>user/login" method="post">
<div class="form-group">
<label for="userName">UserName:label> <input type="text"
class="form-control" id="userName" name="userName"
placeholder="请输入用户名" />
div>
<div class="form-group">
<label for="password">Password:label> <input type="text"
class="form-control" id="passWord" name="passWord"
placeholder="请输入密码" />
div>
<div class="form-group">
<button type="submit" class="btn btn-sm btn-success">Sign
inbutton>
div>
form>
div>
body>
<script src="<%=basePath%>js/jquery.min.js">script>
<script src="<%=basePath%>bootstrap/js/bootstrap.min.js">script>
html>