目前俺在搭建一个自己的个人网站玩玩,一边练习,一边把用到的技术总结一下,日后好复习。
网站框架大致如下图所示:
目前只用到了SpringMVC+Spring+Mybatis+Mysql,把它弄到了自己的服务器上来玩耍。
后台结构图:
目前主要分为:
- 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