org.mybatis
mybatis
3.2.7
org.mybatis
mybatis-spring
1.2.2
proxool
proxool
0.9.1
proxool
proxool-cglib
0.9.1
com.oracle
ojdbc6
11.2.2
org.springframework
spring-orm
4.2.3.RELEASE
#数据源配置
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@xxx.xxx.XXX.XXX:1521:xxxx
jdbc.username=xxx
jdbc.password=xxx
这样我们在进行mybaits相关配置时,就可以直接引用这些内容了。为了能够在xml里通过${}读取properties文件内容,我们还要配置profile。在resources目录新建applicationContext-profile.xml文件,内容如下:
classpath:config/config_#{systemProperties['run_env']}.properties
l ocations里面列出了配置文件位置,而且通过run_env来读取不同的配置文件。这样就可以在测试环境读取测试环境的配置(例如config_test.properties)、生产环境读取生产环境的配置(例如config_prod.properties),tomcat启动没有定义run_env则使用默认的配置文件config_.properties。
数据源配置
首先是定义DataSource,它表示用于连接数据库的数据源。其中重要的属性我们都放在了config_.properties文件中,通过${}来引用。
其次就是我们的mybatis sqlSessionFactoryBean,在定义SqlSessionFactoryBean的时候,dataSource属性是必须指定的,其它属性的含义如下:
至此,配置告一段落,可以开始进行登录验证的开发了。
--测试表
DROP TABLE USER_TEST CASCADE CONSTRAINTS;
CREATE TABLE USER_TEST
(
ID VARCHAR2(32) NOT NULL,
LOGIN_NAME VARCHAR2(10),
PASSWORD VARCHAR2(128),
REAL_NAME VARCHAR2(50),
CREATE_DATE DATE,
UPDATE_DATE DATE
)
ALTER TABLE USER_TEST ADD CONSTRAINT PK_USER_ID PRIMARY KEY (ID)
然后插入一条测试数据:
insert into user_test values('A43G7KHHML903ADF','gameloft9','123456','vivian',sysdate,sysdate)
然后开始编写model、dao和mapper文件:
package com.xxx.demo.dataaccess.model;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* Created by gameloft9 on 2017/11/28.
*/
@Data
public class UserTest implements Serializable{
private static final long serialVersionUID = 1L;
/**id*/
private String id;
/**登录名*/
private String loginName;
/**密码*/
private String password;
/**真实姓名*/
private String realName;
/**创建时间*/
private Date createTime;
/**更新时间*/
private Date updateTime;
}
package com.xxx.demo.dataaccess.dao;
import org.apache.ibatis.annotations.Param;
/**
* Created by gameloft9 on 2017/11/28.
*/
public interface UserMapper {
/**
* 查找用户
* @param loginName 登录名
* @param password 密码(为了不增加复杂度,这里不进行加密,使用明文)
* */
Integer countUserByNameAndPwd(@Param("loginName") String loginName,
@Param("password") String password);
}
ID,LOGIN_NAME,PASSWORD,REAL_NAME,CREATE_TIME,UPDATE_TIME
package com.xxx.demo.dataaccess.api;
/**
* Created by gameloft9 on 2017/11/28.
*/
public interface UserService {
/**
* 验证用户
* @param loginName 登录名
* @param password 密码(为了不增加复杂度,这里不进行加密,使用明文)
* */
Boolean validateUser(String loginName,String password);
}
package com.xxx.demo.dataaccess.impl;
import com.xxx.demo.dataaccess.api.UserService;
import com.xxx.demo.dataaccess.dao.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by gameloft9 on 2017/11/28.
*/
@Service
@Slf4j
public class UserServiceImpl implements UserService {
@Autowired
UserMapper dao;
/**
* 验证用户
* @param loginName 登录名
* @param password 密码(为了不增加复杂度,这里不进行加密,使用明文)
* */
public Boolean validateUser(String loginName,String password){
int n = dao.countUserByNameAndPwd(loginName,password);
return n>0?true:false;
}
}
package com.xxx.demo.controllers;
import com.xxx.demo.dataaccess.api.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
/**
* Created by gameloft9 on 2017/11/27.
*/
@Slf4j
@Controller
public class LoginController {
@Autowired
UserService userServiceImpl;
/**
* 登录页面入口
*/
@RequestMapping(value = "/index.do", method = RequestMethod.GET)
public String index(Model model, HttpServletResponse response) {
log.info("进入登录页面");
return "login";
}
/**
* 登录请求
*/
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public String login(Model model, String name, String pwd) {
if(StringUtils.isBlank(name)||StringUtils.isBlank(pwd)){
log.info("用户名和密码为空");
model.addAttribute("errInfo", "用户名或密码不能为空!");
return "login";
}
Boolean passed = userServiceImpl.validateUser(name,pwd);
if(passed){
log.info("验证成功!");
model.addAttribute("name", name);
return "home";
}
log.info("用户名密码错误,name:{},pwd:{}",name,pwd);
model.addAttribute("errInfo", "用户名或密码错误!");
return "login";
}
}
package com.xxx.demo.dataaccess.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import java.sql.Connection;
import java.util.Properties;
/**
* SQL拦截器,控制日志打印
* @author gameloft9
* 2017-11-29
*/
@Slf4j
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class DialectStatementHandlerInterceptor implements Interceptor {
/**是否开启debug模式*/
private String debug;
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler statement = (RoutingStatementHandler) invocation
.getTarget();
if ("true".equals(debug)) {
log.info("Executing SQL: {}", statement.getBoundSql().getSql().replaceAll("\\s+", " "));
log.info("\twith params: {}", statement.getBoundSql().getParameterObject());
}
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
this.debug = (String) properties.getProperty("debug");
}
}