maven spring springmvc mybatis搭建

来源:互联网 发布:ecshop微商城源码 编辑:程序博客网 时间:2024/05/18 13:31
本文采用注解方式,前台使用angularjs。后续将前台jsp页面改成html。使用angularjs的路由定向页面。
文字概略:
1.创建maven项目在另外一篇博文有创建流程。
2.配置web.xml,设置加载spring的bean容器和监听器,配置springmvc的servlet。
3.src/main/resources下面放置配置文件
(1).applicationContext.xml
(2).spring-mvc.xml
(3).jdbc.properties

(4).log4j.properties

4.在java源包下创建controller,service,dao,mapper,entity,util等工程需要的包。

5.前台通过ajax发送请求经controller进行逻辑处理返回页面或者返回json串。一般返回json串,跳转由前台框架进行控制。

1.工程结构图


2.相关配置文件

(1)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/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.website</groupId>  <artifactId>mavenProject</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>    <properties>        <!-- spring版本号 -->        <spring.version>4.3.3.RELEASE</spring.version>        <!-- log4j日志文件管理包版本 -->        <slf4j.version>1.6.6</slf4j.version>        <log4j.version>1.2.12</log4j.version>        <!-- junit版本号 -->        <junit.version>4.10</junit.version>        <!-- mybatis版本号 -->        <mybatis.version>3.2.1</mybatis.version>    </properties>    <dependencies>        <!-- 添加Spring依赖 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <!--单元测试依赖 -->        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>${junit.version}</version>            <scope>test</scope>        </dependency>        <!-- 日志文件管理包 -->        <!-- log start -->        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>${log4j.version}</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>${slf4j.version}</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>${slf4j.version}</version>        </dependency>        <!-- log end -->        <!--spring单元测试依赖 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>            <scope>test</scope>        </dependency>        <!--mybatis依赖 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>${mybatis.version}</version>        </dependency>        <!-- mybatis/spring包 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.2.0</version>        </dependency>        <!-- mysql驱动包 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.29</version>        </dependency>                <!-- c3p0连接池 -->        <dependency>    <groupId>c3p0</groupId>    <artifactId>c3p0</artifactId>    <version>0.9.1.2</version>        </dependency>    </dependencies></project>

(2).web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>mavenProject</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><filter><!-- 处理文字请求 --><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><!-- 初始化mvc容器 --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 初始化bean容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

(3).applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><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:tx="http://www.springframework.org/schema/tx"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.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 排除扫描controller --><context:component-scan base-package="com.mavenPro"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 加载配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- SqlSessionFactory配置 (mybatis-spring.jar包里面的) --><bean id="mybatisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据源 --><property name="dataSource" ref="dataSource"></property><!-- 配置所有实体对象的别名 --><property name="typeAliasesPackage" value="com.mavenPro.entity"></property><!-- 所有的Mapper文件 --><property name="mapperLocations" value="classpath:mapping/*.xml"></property></bean><!-- Dao(所有Dao的配置) --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.mavenPro.dao"></property><property name="sqlSessionFactoryBeanName" value="mybatisSqlSessionFactory"></property></bean><!-- 事务管理器 --><!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> --></beans>  
(4).spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><mvc:annotation-driven /><!-- 扫描只包含controller --><context:component-scan base-package="com.mavenPro"use-default-filters="false"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 映射静态资源 --><mvc:resources location="/views/images/" mapping="images/**"></mvc:resources><mvc:resources location="/views/css/" mapping="css/**"></mvc:resources><mvc:resources location="/views/js/" mapping="js/**"></mvc:resources><mvc:resources location="/views/3rdparty/" mapping="3rdparty/**"></mvc:resources><!-- 视图解析器 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/page"></property><property name="suffix" value=".jsp"></property></bean></beans>  

(5).log4j.properties
log4j.rootLogger=DEBUG, stdoutlog4j.logger.org.mybatis=DEBUGlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

前台代码

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><link rel="stylesheet" href="3rdparty/bootstrap/css/bootstrap.min.css"><link rel="stylesheet" href="css/index.css"></head><body>    <div class="indexDiv" ng-controller="indexCtr">        <span class="indexText">登陆入口</span>        <table>            <tr>                <td> <span  class="glyphicon glyphicon-user"></span></td>                <td><input  type="text"  ng-model="table.name" class="form-control"></td>            </tr>            <tr>                <td><span class="glyphicon glyphicon-lock"></span></td>                <td><input  type="text"  ng-model="table.password" class="form-control" ></td>            </tr>            <tr>                <td colspan="2">                <input type="submit" ng-bind="table.loginName" class="btn btn-primary" ng-click="sendSubmit()">                <input type="reset"  ng-bind="table.loginReset" class="btn btn-danger">            </tr>        </table>    </div></body><script src="3rdparty/angular/angular.min.js"></script><script src="3rdparty/jquery/jquery-3.2.1.min.js"></script><script src="3rdparty/bootstrap/js/bootstrap.min.js"></script><script src="js/controller/index.js"></script></html>
index.css

.indexDiv{    width:300px;    height: 220px;    background-color: azure;    margin:80px auto;}.indexDiv table{position:relative;border-spacing:0px 15px;border-collapse:separate;top:30px;left:20px;}.indexText{    position:relative;font-weight:bold;font-family: "微软雅黑";    font-size: 20px;    left: 110px;    top: 20px;    color:#ff9955;    }tr:last-child input{    margin-left:50px;}tr:nth-last-child(2) input{    margin-left:10px;}tr:nth-last-child(3) input{    margin-left:10px;}
index.js

var app=angular.module("myApp",[]);app.controller("indexCtr", function($scope,$http) {    $scope.table = {        loginName : '登陆',        loginReset : '重置'    };    $scope.sendSubmit = function(){        var params={            userName:$scope.userName,            userPassword:$scope.userPassword        };        $http({        method:'POST',        url:'a/login',            data:params        }).success(function(response){        $scope.table.loginReset='成功';        }).error(function(response){            console.log(response);        })    }});
后台代码

UserController.java

package com.mavenPro.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;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.ResponseBody;import com.mavenPro.entity.User;import com.mavenPro.service.UserService;@Controller@RequestMapping("/a")public class UserController {    @Autowired    private UserService userService;        @RequestMapping(value="/login", method=RequestMethod.POST)    @ResponseBody    public User validateLogin(User user)    {       return  userService.selectStuInfoById(user);    }}
UserDao.java

package com.mavenPro.dao;import com.mavenPro.entity.User;public interface UserDao {        public User selectStuInfoById(User user);    }
User.java

package com.mavenPro.entity;public class User {    private int userId;    private String userName;    private String userPassword;    private int userAge;    public int getUserId() {        return userId;    }    public void setUserId(int userId) {        this.userId = userId;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getUserPassword() {        return userPassword;    }    public void setUserPassword(String userPassword) {        this.userPassword = userPassword;    }    public int getUserAge() {        return userAge;    }    public void setUserAge(int userAge) {        this.userAge = userAge;    }}
UserService.java

package com.mavenPro.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.mavenPro.dao.UserDao;import com.mavenPro.entity.User;@Service("userService")public class UserService {    @Autowired    private UserDao userDao;        public User selectStuInfoById(User user){        return  userDao.selectStuInfoById(user);    }}
映射文件

userDaoMapper.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.mavenPro.dao.UserDao"><select id="selectStuInfoById" resultType="int">select userIdfrom tbl_user_Infowhere userName = #{userName} userPassword=#{userPassword}</select></mapper>
登陆页面效果图






原创粉丝点击