眼下俺在搭建一个自己的个人站点玩玩。一边练习。一边把用到的技术总结一下,日后好复习。

站点框架大致例如以下图所看到的:



眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql。把它弄到了自己的server上来玩耍。

后台结构图:



眼下主要分为:

  • view–controller层与前台交互,登陆拦截器
  • utils–工具类。一些经常使用的工具类。
  • interceptor–拦截器。页面请求地址拦截和预防XSS漏洞拦截。

  • impl–接口类,Service层-进行业务处理。

  • exception–异常处理器。自己定义异常处理,拦截。

  • entity–实体类,存放部分数据库相关实体。
  • dao–与数据库进行交互,获取数据。
  • 眼下缺少一个封装类包。

具体演示样例代码:

  • TestController.java插入和依据ID获取数据
package com.YouXu.view;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.YouXu.entity.Test;
import com.YouXu.impl.TestImpl;
import com.YouXu.utils.IpAddr; /**
*
* @Title: TestController.java
* @Package: com.YouXu.view
* @author you.xu
* @date 2015年12月21日下午4:38:04
* @version 1.0
*/
@Controller
public class TestController { private static final Logger log = Logger.getLogger(TestController.class); @Autowired
private TestImpl testImpl; @RequestMapping("test")
@ResponseBody
public Test test(@RequestParam("id") Integer id) {
Test test = new Test();
try {
test = testImpl.getTest(id);
} catch (Exception e) {
e.printStackTrace();
log.error(e, e);
}
return test;
} @RequestMapping("getIndex")
@ResponseBody
public void index(HttpServletRequest request) {
String iip = null;
try {
iip = IpAddr.getIpAddr(request);
} catch (Exception e) {
log.error(e, e);
e.printStackTrace();
}
testImpl.addTest(iip);
}
}
  • TestImpl.java
package com.YouXu.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.YouXu.dao.TestDao;
import com.YouXu.entity.Test; /**
*
* @Title: TestImpl.java
* @Package: com.YouXu.impl
* @author you.xu
* @date 2015年12月21日下午4:40:46
* @version 1.0
*/
@Service
public class TestImpl { @Autowired
private TestDao testDao; public Test getTest(Integer id) { Test test = testDao.getTest(id);
return test;
} public void addTest(String iip) {
testDao.addTest(iip);
} }
  • TestDao.java
package com.YouXu.dao;

import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.YouXu.entity.Test; /**
*
* @Title: TestDao.java
* @Package: com.YouXu.dao
* @author you.xu
* @date 2015年12月21日下午4:40:56
* @version 1.0
*/
@Repository
public class TestDao extends SqlSessionDaoSupport { @Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
} String ns = "com.YouXu.sqlMap.TestMapper."; public Test getTest(Integer id) {
Test test = this.getSqlSession().selectOne(ns + "getTestbyId", id);
return test;
} public void addTest(String iip) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = formatter.format(new Date());
Test test = new Test();
test.setIip(iip);
test.setTtime(time);
this.getSqlSession().insert(ns + "insertTest", test);
} }
  • Test.java
package com.YouXu.entity;

/**
*
* @Title: Test.java
* @Package: com.YouXu.entity
* @author you.xu
* @date 2015年12月21日下午4:39:00
* @version 1.0
*/
public class Test { private Integer iid;
private String iip;
private String ttime; public Integer getIid() {
return iid;
} public void setIid(Integer iid) {
this.iid = iid;
} public String getIip() {
return iip;
} public void setIip(String iip) {
this.iip = iip;
} public String getTtime() {
return ttime;
} public void setTtime(String ttime) {
this.ttime = ttime;
} public String toString() {
return "Test [iid=" + iid + ", iip=" + iip + ", ttime=" + ttime + "]";
} public Test(Integer iid, String iip, String ttime) {
super();
this.iid = iid;
this.iip = iip;
this.ttime = ttime;
} public Test() {
super();
} }

配置文件配置:

  • springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config />
<mvc:annotation-driven />
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.YouXu" /> <!-- 配置过滤静态文件 -->
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/audio/**" location="/WEB-INF/audio/" />
<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
<mvc:resources mapping="/**" location="/index.html" />
<!-- 启动JSON格式的配置 -->
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable
representation -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 上传文件时须要这一段代码 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
</bean>
<!-- 定义统一异常处理器 -->
<bean class="com.YouXu.exception.CustomExceptionResolver"></bean> <!-- 注解处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
<!-- 注解适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
</bean> <!-- 返回视图 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 配置有须要登录的请求进行拦截 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/login/*" />
<bean class="com.YouXu.view.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
  • applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://************:3306/******" />
<property name="username" value="****" />
<property name="password" value="****" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:advice id="txAdive" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:advisor advice-ref="txAdive"
pointcut="execution(* com.YouXu.impl..*.*(..))" />
</aop:config>
</beans>
  • sqlMapConfig.xml
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="com/YouXu/sqlMap/TestMapper.xml" />
</mappers>
</configuration>
  • log4j.properties
# 设定logger的root level为INFO,指定的输出目的地(appender)为file,并在控制台输出stdout(Console)
log4j.rootLogger=info, file, stdout
# 设定stdout控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{HH\:mm\:ss}] %5p %F\:%L "%m"%n
# 设定输出位置。此处设定tomcat文件夹的logs下,文件名称为projectLogs.log。
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.append = true
log4j.appender.file.encoding=UTF-8
log4j.appender.file.File=${catalina.home}/logs/youxu.log
log4j.appender.file.datePattern='.'yyyy-MM-dd
log4j.appender.file.BufferedIO=true
log4j.appender.file.BufferSize=8192
# 设定制定的file使用的PatternLayout.
# 有关ConversionPattern中的转意字符的含义參考说明
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p [%C:%M:%L] %m%n
  • TestMapper.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.YouXu.sqlMap.TestMapper"> <resultMap id="BaseResultMap" type="com.YouXu.entity.Test">
<id column="iid" property="iid" />
<result column="iip" property="iip" />
<result column="ttime" property="ttime" />
</resultMap>
<sql id="Base_Column_List">
iid,iip,ttime
</sql>
<select id="getTestbyId" parameterType="Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from Test
where iid = #{iid}
</select> <insert id="insertTest" parameterType="com.YouXu.entity.Test">
<selectKey keyProperty="iid" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into test(iid,iip,ttime)
values(null,#{iip},#{ttime})
</insert> </mapper>
  • web.xml
<?

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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>YouXu</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- 配置spring容器监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>XSSFilter</filter-name>
<filter-class>com.YouXu.interceptor.XSSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XSSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 载入spring配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件的地址 假设不配置contextConfigLocation, 默认查找的配置文件名称称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml -->
<param-value>classpath:springmvc.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<!-- 能够配置/ 。此工程 全部请求全部由springmvc解析,此种方式能够实现 RESTful方式,须要特殊处理对静态文件的解析不能由springmvc解析
能够配置*.do或*.action,全部请求的url扩展名为.do或.action由springmvc解析,此种方法经常使用 不能够/*,假设配置/*,返回jsp也由springmvc解析,这是不正确的。 -->
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- post乱码处理 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<session-config>
<session-timeout>15</session-timeout>
</session-config> <welcome-file-list>
<welcome-file>index-h5.html</welcome-file>
</welcome-file-list>
</web-app>

所需用到的lib包:点击此处

眼下应该就是这些。日后有补充的在进行加入。

我是一个小菜鸟,大家如有更简单高效的配置搭建。欢迎在评论中指出,一起分享。谢谢

SpringMVC+Spring+Mybatis+Mysql项目搭建的更多相关文章

  1. Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)

    这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...

  2. eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建

    这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 接下来马上进入项目搭建 ...

  3. springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    @_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...

  4. maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...

  5. Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建

    http://blog.csdn.net/u013142781/article/details/50380920

  6. (一)springmvc+spring+mybatis+maven框架搭建

    (一)springmvc+spring+mybatis+maven框架搭建 1.说明 工作之余,为了学习点东西.先搭建个框架. 以后要往里面加东西,比如rabbitMQ.redis.shiro等. 也 ...

  7. SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

    转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...

  8. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

    自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...

  9. springmvc+spring+mybatis+maven项目构建

    1.首先在myeclipse10中安装maven的插件,将插件放入D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropin ...

随机推荐

  1. hdu1874 畅通project续 最短路 floyd或dijkstra或spfa

    Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择.而某些方案 ...

  2. Android开发经验之点击图片判断是否在图片范围之内

    package xiaosi.grivaty; import android.content.Context; import android.graphics.Bitmap; import andro ...

  3. DG动态性能视图详解

    V$LOG 显示CONTROLFILE记录的LOG FILE信息. 列名          描述 GROUP#        日志组号 THREAD#       日志线程号 SEQUENCE#    ...

  4. BZOJ2119: 股市的预测(后缀数组)

    Description 墨墨的妈妈热爱炒股,她要求墨墨为她编写一个软件,预测某只股票未来的走势.股票折线图是研究股票的必备工 具,它通过一张时间与股票的价位的函数图像清晰地展示了股票的走势情况.经过长 ...

  5. Python数据类型中的字符串类型

    1.换行字符:\n print ('I love python.\nAnd you?') 2.转义字符(\):\\ print ('\\\n\\') 3.制表字符(对齐表格的各列):\t print ...

  6. RS-485总线和Modbus通信协议的关系

    一.RS-485总线 RS-485总线技术只是规定了接口的电气标准,并没有规定RS-485接口的电缆,插件以及通信协议,只是OSI规范中物理层的一个标准,RS-485总线采用差分平衡传输方式.由于RS ...

  7. 洛谷 P1610 鸿山洞的灯

    P1610 鸿山洞的灯 题目描述 已知n盏灯以及每盏灯的位置p[i],p[i]均不相等,两盏相邻的灯当小于dist时,若这个安全距离里面还有灯是亮着时,就可以关掉该盏灯,(即若第i-1盏与第i+1盏的 ...

  8. 找不到或无法载入主类 org.jivesoftware.openfire.starter.ServerStarter

    刚接触openfire的配置就出现了这个错误.解决方法非常easy,忘记了将openfire的源文件加入到user entries中了

  9. hdu4605Magic Ball Game 树状数组

    //给一棵树.树的每个节点的子节点个数是0或2 //对于每个节点都有一个权值w[i] //一个权值为x的球在每个节点的情况有 //x=w[i] 这个球在该点不向下掉 //x<w[i] 这个球往左 ...

  10. jquery模拟可输入的下拉框

    //页面html <div id="select" class="select" > <ul> <c:forEach items= ...