Java高级架构JAVASpring Boot

spring+springMVC+mybatis的框架项目基础环

2018-06-02  本文已影响10人  Java高级架构

一、 搭建目标:

实现标准的后台controller、service、dao三层结构;

使用mapper.xml配置文件实现dao层和数据库的交互;

数据库连接信息、基础配置文件存在config.properties文件中;

配置日志打印相关的信息;

配置数据库连接池;

使用注解;

配置json数据前后台交互;

使用junit测试;

二、环境基础:

eclipe4.4.1;

maven3.2.5;

spring4.0.3;

mysql5.6;

jdk1.7;

tomcat7;

Angularjs1.4.0;

注:为了确定后台环境是真的没有问题,自然也需要简单搭一下前台的环境,起码要能保证前后台交互没有问题。因此我前台也简单的弄了一下angularjs,实际上我们项目中前端环境已经不这么搭里 ,只是新的搭法我还不会。

三、项目整体结构如下:

1、 后端:

2、 前端:

四、maven导包及基础配置pom.xml代码:

[html] view plain copy

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

4.0.0  

frameTest  

frameTest  

war  

0.0.1-SNAPSHOT  

frameTest Maven Webapp  

http://maven.apache.org  

  

  

  

maven-compiler-plugin  

2.3.2  

  

1.7  

1.7  

UTF-8  

  

  

${java.home}\lib\rt.jar  

  

  

src/main/java  

  

**/*.xml  

  

src/main/resources  

  

**/*.*  

frameTest  

  

4.0.3.RELEASE  

  

  

org.springframework  

spring-test  

${spring-version}  

  

org.springframework  

spring-webmvc  

${spring-version}  

  

org.springframework  

spring-core  

${spring-version}  

  

org.springframework  

spring-context  

${spring-version}  

  

org.springframework  

spring-context-support  

${spring-version}  

  

org.springframework  

spring-beans  

${spring-version}  

  

org.springframework  

spring-orm  

${spring-version}  

  

org.springframework.amqp  

spring-amqp  

1.2.0.RELEASE  

  

org.aspectj  

aspectjweaver  

1.7.1  

  

org.mybatis  

mybatis  

3.2.7  

  

org.mybatis  

mybatis-spring  

1.2.2  

  

com.alibaba  

druid  

1.0.2  

  

mysql  

mysql-connector-java  

5.1.34  

  

junit  

junit  

4.11  

test  

  

org.codehaus.jackson  

jackson-mapper-asl  

1.9.7  

  

com.alibaba  

fastjson  

1.1.26  

  

org.codehaus.jackson  

jackson-core-asl  

1.9.7  

  

javax.servlet  

servlet-api  

3.0-alpha-1  

provided  

  

org.apache.geronimo.specs  

geronimo-servlet_3.0_spec  

1.0  

test  

  

cglib  

cglib  

2.2  

  

ch.qos.logback  

logback-classic  

1.0.9  

  

commons-io  

commons-io  

2.4  

  

com.github.snakerflow  

snaker-core  

2.5.1  

  

org.apache.commons  

commons-lang3  

3.3.1  

五、web项目基础配置文件web.xml:

[html] view plain copy

  

appversion  

  

contextConfigLocation  

classpath:spring.xml  

  

字符集过滤器  

encodingFilter  

org.springframework.web.filter.CharacterEncodingFilter  

  

字符集编码  

encoding  

UTF-8  

  

encodingFilter  

/*  

  

spring监听器  

org.springframework.web.context.ContextLoaderListener  

  

org.springframework.web.util.IntrospectorCleanupListener  

  

dispatcher  

org.springframework.web.servlet.DispatcherServlet  

  

contextConfigLocation  

classpath:spring-mvc.xml  

1  

  

dispatcher  

/  

  

15  

六、spring基础篇日志文件spring.xml:

[html] view plain copy

xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  

xsi:schemaLocation="http://www.springframework.org/schema/beans  

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

http://www.springframework.org/schema/context  

http://www.springframework.org/schema/context/spring-context-3.0.xsd  

http://www.springframework.org/schema/task  

http://www.springframework.org/schema/task/spring-task-3.1.xsd  

http://www.springframework.org/schema/tx  

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  

  

  

  

  

  

  

七、mybatis基础配置文件spring-MybatisConfig.xml:  

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">  

  

    

    

    

     

    

    

    

    

    

    

    

    

    

    

  

  

  

value="classpath*:merDao/mapper/*Mapper.xml">  

  

  

  

  

七、mybatis基础配置文件MybatisConf.xml:

[html] view plain copy

  

  

  

  

  

  

   

    

  

  

八、springMVC基础配置文件spring-mvc.xml:

[html] view plain copy

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">  

     

  

  

  

  

  

text/html;charset=UTF-8  

class = "org.springframework.http.converter.StringHttpMessageConverter"/>  

  

  

  

  

  

九、日志打印相关配置logback.xml:

[html] view plain copy

  

    

  

  

%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n  

     

  

${LOG_HOME}//merchant.%d{yyyy-MM-dd}.log  

30  

  

%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n  

  

  

  

  

  

    

    

    

    

    

  

  

  

十、属性文件config.properties:

[html] view plain copy

#mysql  

jdbc.driverClassName=com.mysql.jdbc.Driver  

jdbc.url=jdbc:mysql://127.0.0.1:3306/merchant?useUnicode=true&characterEncoding=utf-8  

jdbc.username=root  

jdbc.password=123456  

#c3p0  

c3p0.minPoolSize=5  

c3p0.initialPoolSize=10  

c3p0.maxIdleTime=60  

c3p0.acquireIncrement=5  

c3p0.idleConnectionTestPeriod=60  

c3p0.acquireRetryAttempts=30  

c3p0.acquireRetryDelay=1000  

c3p0.numHelperThreads=3  

c3p0.breakAfterAcquireFailure=true  

c3p0.testConnectionOnCheckout=false  

c3p0.maxStatements=0  

c3p0.maxStatementsPerConnection=0  

#druid 阿里巴巴提供的JDBC连接池、监控组件  

druid.minPoolSize=5  

druid.maxPoolSize=30  

druid.initialPoolSize=10  

druid.maxIdleTime=60  

druid.acquireIncrement=5  

druid.idleConnectionTestPeriod=60  

druid.acquireRetryAttempts=30  

druid.acquireRetryDelay=1000  

druid.numHelperThreads=3  

druid.breakAfterAcquireFailure=true  

druid.testConnectionOnCheckout=false  

druid.maxStatements=0  

druid.maxStatementsPerConnection=0  

druid.maxWait=60000  

druid.timeBetweenEvictionRunsMillis=3000  

druid.minEvictableIdleTimeMillis=300000  

druid.maxPoolPreparedStatementPerConnectionSize=20  

druid.validationQuery=SELECT 'x'  

druid.testWhileIdle=true  

druid.testOnBorrow=false  

druid.testOnReturn=false  

druid.poolPreparedStatements=false  

十一、为了验证这些配置是否可行,我写了一个简单的增加用户信息的操作,代码分别如下:

后台实体类userModel:

[java] view plain copy

package merModel;  

import java.io.Serializable;  

public class UserModel implements Serializable {  

private static final long serialVersionUID = -3291196087479862240L;  

private int id;  

/**

     * 用户账号

     */  

private String account;  

/**

     * 用户姓名

     */  

private String userName;  

/**

     * 用户密码

     */  

private String password;  

/**

     * 手机

     */  

private String mobile;  

/**

     * 邮箱

     */  

private String email;  

public String getAccount() {  

return account;  

    }  

public void setAccount(String account) {  

this.account = account;  

    }  

public String getUserName() {  

return userName;  

    }  

public void setUserName(String userName) {  

this.userName = userName;  

    }  

public String getPassword() {  

return password;  

    }  

public void setPassword(String password) {  

this.password = password;  

    }  

public String getMobile() {  

return mobile;  

    }  

public void setMobile(String mobile) {  

this.mobile = mobile;  

    }  

public String getEmail() {  

return email;  

    }  

public void setEmail(String email) {  

this.email = email;  

    }  

public static long getSerialversionuid() {  

return serialVersionUID;  

    }  

public int getId() {  

return id;  

    }  

public void setId(int id) {  

this.id = id;  

    }  

@Override  

public String toString() {  

return "UserModel [id=" + id + ", account=" + account + ", userName="  

+ userName +", password=" + password + ", modile=" + mobile  

+", email=" + email + "]";  

    }  

}  

对应的前台UserCommand类:

[java] view plain copy

package merCommand;  

public class UserCommand {  

private int id;  

/**

     * 用户账号

     */  

private String account;  

/**

     * 用户姓名

     */  

private String userName;  

/**

     * 用户密码

     */  

private String password;  

/**

     * 手机

     */  

private String mobile;  

/**

     * 邮箱

     */  

private String email;  

public String getAccount() {  

return account;  

    }  

public void setAccount(String account) {  

this.account = account;  

    }  

public String getUserName() {  

return userName;  

    }  

public void setUserName(String userName) {  

this.userName = userName;  

    }  

public String getPassword() {  

return password;  

    }  

public void setPassword(String password) {  

this.password = password;  

    }  

public String getMobile() {  

return mobile;  

    }  

public void setMobile(String mobile) {  

this.mobile = mobile;  

    }  

public String getEmail() {  

return email;  

    }  

public void setEmail(String email) {  

this.email = email;  

    }  

public int getId() {  

return id;  

    }  

public void setId(int id) {  

this.id = id;  

    }  

@Override  

public String toString() {  

return "UserCommand [id=" + id + ", account=" + account + ", userName="  

+ userName +", password=" + password + ", modile=" + mobile  

+", email=" + email + "]";  

    }  

}  

Dao接口UserDao:

[java] view plain copy

package merDao;  

import merModel.UserModel;  

public interface UserDao {  

public void save(UserModel usermodel);  

}  

Dao实现userDaoMapper.xml:

[html] view plain copy

  

  

  

  

  

  

  

      

id,account,userName,password,mobile,email  

  

insert into user()  

    values(#{id},#{account},#{userName},#{password},#{mobile},#{email})  

查询用户-->  

  

        SELECT u.* FROM user u   

UserService接口:

[java] view plain copy

package merService;  

import merModel.UserModel;  

public interface UserService {  

public void add(UserModel usermodel);  

}  

userService实现:

[html] view plain copy

package merService.merServiceImp;  

import javax.annotation.Resource;  

import merDao.UserDao;  

import merModel.UserModel;  

import merService.UserService;  

import org.slf4j.Logger;  

import org.slf4j.LoggerFactory;  

import org.springframework.stereotype.Service;  

@Service("UserService")  

public class UserServiceImp implements UserService {  

privatefinal Loggerlogger = LoggerFactory.getLogger(UserServiceImp.class);  

    private UserDao userDao;  

@Resource(name = "userDao")  

    public void setUserDao(UserDao userDao) {  

this.userDao = userDao;  

    }  

    @Override  

    public void add(UserModel usermodel) {  

        userDao.save(usermodel);  

    }  

}  

Controller前后台交互:

[java] view plain copy

package merController;  

import merCommand.UserCommand;  

import merModel.UserModel;  

import merService.UserService;  

import org.springframework.beans.BeanUtils;  

import org.springframework.beans.factory.annotation.Autowired;  

import org.springframework.web.bind.annotation.RequestBody;  

import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.web.bind.annotation.RequestMethod;  

import org.springframework.web.bind.annotation.RestController;  

@RestController  

public class UserController {  

@Autowired  

private UserService userService;  

/**

     * 新增用户

     * 

     * @author:tuzongxun

     * @Title: addUser

     * @param @param userCommand

     * @return void

     * @date Apr 14, 2016 11:28:47 AM

     * @throws

     */  

@RequestMapping(value = "addUser", method = RequestMethod.POST)  

public void addUser(@RequestBody UserCommand userCommand) {  

UserModel userModel =new UserModel();  

        BeanUtils.copyProperties(userCommand, userModel);  

        userService.add(userModel);  

    }  

}  

前台index.html文件:

[html] view plain copy

  

  

交易业务商户服务平台  

  

  

  

  

  

  

  

  

  

  

首页  

|  

申告管理  

|  

自助服务  

|  

意见管理  

|  

知识库  

  

  

前台app.js文件代码:

[javascript] view plain copy

var app=angular.module('merchantApp',['ui.router']);    

app.config(function($stateProvider,$urlRouterProvider){    

$urlRouterProvider.otherwise('/addUser');    

    $stateProvider    

.state('addUser', {    

url:"/addUser",    

    views: {    

'view': {    

templateUrl:'views/addUser.html',    

controller:'addUserCtr'    

       }    

    }    

   });    

});    

增加用户信息的界面addUser.html:

[html] view plain copy

  

  

用户名:

  

姓     名:

  

密     码:

  

手     机:

  

邮     箱:

  

    

  

增加用户信息的angularjs控制层:

[javascript] view plain copy

angular.module('merchantApp')    

.controller('addUserCtr', ['$scope','$http', function($scope,$http){    

$scope.addUser=function(user){  

//向后台提交数据  

$http.post("./addUser",user,{headers:'Content-Type:application/json'}).success(function(){   

      });  

    }     

}])    

相关的css:

[css] view plain copy

body{  

margin:0;  

}  

.button1{  

font-size:28px;  

}  

#addUser input{  

font-size:24px;  

}  

#addUser{  

border:2px solid;  

border-radius:0.5em;  

width:35%;  

height:330px;  

float:left;  

margin-left:400px;  

margin-top:100px;  

padding:30px;  

background-image:url("../images/111.jpg");  

background-size:cover;  

font-size:26px;  

}  

#addUser img{  

width:100%;  

height:100%;  

}  

#headBack{  

width:100%;  

height:80px;  

background-color:#E0EEF0;  

position:relative;  

}  

#headDiv{  

width:75%;  

height:5px;  

background:#CBE1E4;  

float:right;  

position:absolute;  

right:0;  

bottom:0;  

}  

#navUl{  

/*position:relative;*/  

position:absolute;  

float:right;  

top:0;  

right:0;  

margin:0;  

    box-sizing:border-box;  

/*background-color:red;*/  

width:75%;  

height:75px;  

}  

#navUl li{    

list-style-type: none;   

/*position:absolute;*/  

font-size:36px;  

display: inline;    

/**top:20;*/  

margin-top:20px;  

left:0;  

height:auto;  

text-color:#2F2E2E;  

/*background-color:#EAE3EA;*/  

text-align:center;  

/**padding:5px;

   margin-top:3px;*/  

}  

a:link{  

text-decoration:none;  

color:black;  

}  

a:visited{  

color:blue;  

}  

a:hover{  

color:red;  

}  

a:active{  

color:orange;  

}  

浏览器访问界面如下:

点击注册以后,数据库信息如下:

至此,基础搭建成功。

上一篇 下一篇

猜你喜欢

热点阅读