一、整合所需jar包:
二、项目目录结构
三、配置文件
1、springmvc的配置文件 springMVC.xml
no
105,179,90
black
250
70
90
KAPTCHA_SESSION_KEY
com.google.code.kaptcha.impl.WaterRipple
5
宋体,楷体,微软雅黑
3、数据库连接参数配置 jdbc.properties
###数据库连接参数配置###
db-driver=oracle.jdbc.driver.OracleDriver
db-url=jdbc:oracle:thin:@localhost:1521:orcl
db-username=pengl
db-password=tiger
4、mybatis配置mybatis-config.xml (仅用来引入sqlmap映射文件)
5、Log4j2配置文件
logs
malone
通用后台管理系统
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
DruidStatView
/druid/*
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:/springMVC.xml,classpath*:/applicationContext-comm.xml
1
springMVC
/
org.apache.logging.log4j.web.Log4jServletContextListener
log4jServletFilter
org.apache.logging.log4j.web.Log4jServletFilter
log4jServletFilter
/*
REQUEST
FORWARD
INCLUDE
ERROR
SpringEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
SpringEncodingFilter
/*
index.jsp
-- Create table
create table TS_TEST
(
id NUMBER,
name VARCHAR2(20),
age NUMBER
)
五、java代码
1、映射实体类
package com.malone.test.dto;
import java.io.Serializable;
public class TestVo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.malone.test.control;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.malone.test.domain.Test;
import com.malone.test.dto.TestVo;
@Controller
@RequestMapping("/test")
public class TestControl {
private Logger _logger = LogManager.getLogger();
@Autowired
@Qualifier("testImpl")
private Test test;
/**
* 测试
* @return
*/
@RequestMapping(value = "/index")
public String index(@RequestParam String id, HttpServletRequest request){
_logger.info("进入Control... , 获取到参数:" + id);
TestVo testbean = test.selectUserById(Integer.parseInt(id));
request.setAttribute("testbean", testbean);
return "index";
}
}
package com.malone.test.domain;
import org.apache.ibatis.annotations.Select;
import com.malone.test.dto.TestVo;
public interface Test {
@Select("select * from ts_test where id = #{id}") //这里可以直接用注解 , 也可以通过配置文件写 ,配置文件需要引入到mybatis-config.xml
public abstract TestVo selectUserById(int id);
}
package com.malone.test.domain;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.malone.test.dto.TestVo;
@Service("testImpl")
public class TestImpl implements Test {
@Autowired
private SqlSessionFactory sessionFactory;
public TestVo selectUserById(int id) {
TestVo testbean = sessionFactory.openSession().selectOne("com.malone.test.domain.Test.selectUserById" ,id);
return testbean;
}
}
4、mybatis映射文件 sqlmap-test.xml //如果在上面的映射类中使用了注解方式,则不需要这个配置文件
5、jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
demo
${testbean.id}
${testbean.name}
${testbean.age}
六、运行结果
Log4j2 日志 和 Mybatis sql日志
阿里数据源监控页面