package com.pengcheng.mybatis.models;
public class BaseBean {
private int enable;
private String created_by;
private String created_time;
private String modified_by;
private String modified_time;
private int is_del;
public int getEnable() {
return enable;
}
public void setEnable(int enable) {
this.enable = enable;
}
public String getCreated_by() {
return created_by;
}
public void setCreated_by(String created_by) {
this.created_by = created_by;
}
public String getCreated_time() {
return created_time;
}
public void setCreated_time(String created_time) {
this.created_time = created_time;
}
public String getModified_by() {
return modified_by;
}
public void setModified_by(String modified_by) {
this.modified_by = modified_by;
}
public String getModified_time() {
return modified_time;
}
public void setModified_time(String modified_time) {
this.modified_time = modified_time;
}
public int getIs_del() {
return is_del;
}
public void setIs_del(int is_del) {
this.is_del = is_del;
}
public BaseBean(int enable, String created_by, String created_time, String modified_by, String modified_time,
int is_del) {
super();
this.enable = enable;
this.created_by = created_by;
this.created_time = created_time;
this.modified_by = modified_by;
this.modified_time = modified_time;
this.is_del = is_del;
}
public BaseBean() {
super();
}
}
=====================================mybatis========================
=====================================接口==========================
package com.pengcheng.mybatis.dao;
import com.pengcheng.mybatis.models.Dept;
public interface IDept {
public Dept getDept(int id);
}
======================================================================================================================================================================================
package com.pengcheng.mybatis.dao;
import java.util.List;
import com.pengcheng.mybatis.models.User;
public interface IUser {
public List<User> getUserList();
public User getUser(String um);
public void insertUser(User user);
public void updateUser(User user);
public void deleteUser(String um);
}
=================================配置conf.xml==========================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.pengcheng.mybatis.models.User"/>
<typeAlias alias="Dept" type="com.pengcheng.mybatis.models.Dept"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.25.165.154)(PORT = 1525))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.25.165.155)(PORT = 1525))(LOAD_BALANCE = yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = hsdspccs)(FAILOVER_MODE =(TYPE = SELECT)(METHOD = BASIC)(RETRIES = 180)(DELAY = 5))))"/>
<property name="username" value="DSPMARKETING" />
<property name="password" value="paic1234" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="config/mapper/User.xml"/>
<mapper resource="config/mapper/Dept.xml"/>
</mappers>
</configuration>
=======================================Dept.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.pengcheng.mybatis.dao.IDept">
<resultMap type="Dept" id="resultDeptMap">
<result property="id" column="id" />
<result property="dept_name" column="dept_name" />
<result property="description" column="description" />
<result property="ps_number" column="ps_number" />
<collection property="user" ofType="com.pengcheng.mybatis.models.User">
<id property="dept_id" column="dept_id" javaType="String" jdbcType="VARCHAR"/>
<result property="user_name" column="user_name" javaType="string" jdbcType="VARCHAR"/>
<result property="um" column="um" javaType="string" jdbcType="VARCHAR"/>
<result property="email" column="email" javaType="string" jdbcType="VARCHAR"/>
<result property="enable" column="enable" javaType="int" jdbcType="INTEGER"/>
</collection>
</resultMap>
<select id="getDept" resultMap="resultDeptMap" parameterType="int">
select d.id,d.dept_name,d.description,d.ps_number,u.* from market_dept d ,MARKET_USER u where d.id=u.dept_id and id= #{id}
</select>
</mapper>
=============================================User.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.pengcheng.mybatis.dao.IUser">
<select id="getUserList" resultType="User">
select * from MARKET_USER
</select>
<select id="getUser" parameterType="String" resultType="User">
select * from MARKET_USER where um = #{um}
</select>
<insert id="insertUser" parameterType="User">
INSERT INTO MARKET_USER(um,
user_name, dept_id,email,enable,created_by,modified_by)
VALUES(#{um}, #{user_name}, #{dept_id}, #{email},#{enable},#{created_by},#{modified_by})
</insert>
<update id="updateUser" parameterType="User">
UPDATE MARKET_USER
SET
email=
#{email}
WHERE
um = #{um}
</update>
<delete id="deleteUser" parameterType="String">
DELETE FROM MARKET_USER WHERE um = #{um}
</delete>
</mapper>
=======================================log4j.properties=======================================
# Set root logger level to DEBUG and its only appender to A1
#log4j\u4E2D\u6709\u4E94\u7EA7logger
#FATAL 0
#ERROR 3
#WARN 4
#INFO 6
#DEBUG 7
#\u914D\u7F6E\u6839Logger\uFF0C\u5176\u8BED\u6CD5\u4E3A\uFF1A
#log4j.rootLogger = [ level ] , appenderName, appenderName, \u2026
log4j.rootLogger=debug, A1 ,R
log4j.logger.org.apache.http=ERROR
#\u8FD9\u4E00\u53E5\u8BBE\u7F6E\u4EE5\u4E3A\u7740\u6240\u6709\u7684log\u90FD\u8F93\u51FA
#\u5982\u679C\u4E3Alog4j.rootLogger=WARN, \u5219\u610F\u5473\u7740\u53EA\u6709WARN,ERROR,FATAL
#\u88AB\u8F93\u51FA\uFF0CDEBUG,INFO\u5C06\u88AB\u5C4F\u853D\u6389.
# A1 is set to be a ConsoleAppender.
#log4j\u4E2DAppender\u6709\u51E0\u5C42\u5982\u63A7\u5236\u53F0\u3001\u6587\u4EF6\u3001GUI\u7EC4\u4EF6\u3001\u751A\u81F3\u662F\u5957\u63A5\u53E3\u670D\u52A1\u5668\u3001NT\u7684\u4E8B\u4EF6\u8BB0\u5F55\u5668\u3001UNIX Syslog\u5B88\u62A4\u8FDB\u7A0B\u7B49
#ConsoleAppender\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 \u4F7F\u7528\u7684\u8F93\u51FA\u5E03\u5C40\uFF0C\u5176\u4E2Dlog4j\u63D0\u4F9B4\u79CD\u5E03\u5C40. org.apache.log4j.HTMLLayout\uFF08\u4EE5HTML\u8868\u683C\u5F62\u5F0F\u5E03\u5C40\uFF09
#org.apache.log4j.PatternLayout\uFF08\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u5E03\u5C40\u6A21\u5F0F\uFF09\uFF0C
#org.apache.log4j.SimpleLayout\uFF08\u5305\u542B\u65E5\u5FD7\u4FE1\u606F\u7684\u7EA7\u522B\u548C\u4FE1\u606F\u5B57\u7B26\u4E32\uFF09\uFF0C
#org.apache.log4j.TTCCLayout\uFF08\u5305\u542B\u65E5\u5FD7\u4EA7\u751F\u7684\u65F6\u95F4\u3001\u7EBF\u7A0B\u3001\u7C7B\u522B\u7B49\u7B49\u4FE1\u606F\uFF09
log4j.appender.A1.Encoding=UTF-8
log4j.appender.A1.layout=org.apache.log4j.SimpleLayout
#\u7075\u6D3B\u5B9A\u4E49\u8F93\u51FA\u683C\u5F0F \u5177\u4F53\u67E5\u770Blog4j javadoc org.apache.log4j.PatternLayout
#d \u65F6\u95F4 ....
#log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
#R \u8F93\u51FA\u5230\u6587\u4EF6 RollingFileAppender\u7684\u6269\u5C55\uFF0C\u53EF\u4EE5\u63D0\u4F9B\u4E00\u79CD\u65E5\u5FD7\u7684\u5907\u4EFD\u529F\u80FD\u3002
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#\u65E5\u5FD7\u6587\u4EF6\u7684\u540D\u79F0${sys:user.home}/.cap/log/cap.log
log4j.appender.R.File=${user.home}/.al/log/al.log
log4j.appender.R.Encoding=UTF-8
#\u65E5\u5FD7\u6587\u4EF6\u7684\u5927\u5C0F
#log4j.appender.R.MaxFileSize=100KB
# \u4FDD\u5B58\u4E00\u4E2A\u5907\u4EFD\u6587\u4EF6
#log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
============================================DeptTest===========================================
package pengcheng;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.annotations.Test;
import com.pengcheng.mybatis.dao.IDept;
import com.pengcheng.mybatis.dao.IUser;
import com.pengcheng.mybatis.models.Dept;
import com.pengcheng.mybatis.models.User;
public class DeptTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("config/conf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//sqlSessionFactory.getConfiguration().addMapper(IUser.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
try {
List<User> users = iuser.getUserList();
for(User user:users){
String userInfo = "UM号:"+user.getUm()+", 姓名:"+user.getUser_name()+", 是否可用:"+user.getEnable();
System.out.println(userInfo);
}
} finally {
session.close();
}
}
@Test
public void testgetUserList(){
SqlSession session = sqlSessionFactory.openSession();
IDept idept = session.getMapper(IDept.class);
try {
Dept dept = idept.getDept(1);
List<User> users = dept.getUser();
System.out.println(users.size());
for(User user:users){
String userInfo = "UM号:"+user.getUm()+", 姓名:"+user.getUser_name()+", 是否可用:"+user.getEnable();
System.out.println(userInfo);
}
} finally {
session.close();
}
}
}
==========================================UserTest=======================================
package pengcheng;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.annotations.Test;
import com.pengcheng.mybatis.dao.IUser;
import com.pengcheng.mybatis.models.User;
public class UserTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("config/conf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//sqlSessionFactory.getConfiguration().addMapper(IUser.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
try {
List<User> users = iuser.getUserList();
for(User user:users){
String userInfo = "UM号:"+user.getUm()+", 姓名:"+user.getUser_name()+", 是否可用:"+user.getEnable();
System.out.println(userInfo);
}
} finally {
session.close();
}
}
@Test
public void testgetUserList(){
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
try {
List<User> users = iuser.getUserList();
for(User user:users){
String userInfo = "UM号:"+user.getUm()+", 姓名:"+user.getUser_name()+", 是否可用:"+user.getEnable();
System.out.println(userInfo);
}
} finally {
session.close();
}
}
@Test
public void testgetUser(){
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
try {
User user = iuser.getUser("LIUDEHUA123");
String userInfo = "UM号:"+user.getUm()+", 姓名:"+user.getUser_name()+", 是否可用:"+user.getEnable();
System.out.println(userInfo);
} finally {
session.close();
}
}
@Test
public void testinsertUser(){
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
User user = new User("LIUDEHUA123","刘德华","1","1@1.com",1,"umright","2018-01-25 18:17:03","umright","2018-01-25 18:17:02",0);
try {
iuser.insertUser(user);
session.commit();
} finally {
session.close();
}
}
@Test
public void testupdateUser(){
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
User user = new User("LIUDEHUA123","刘德华","1","2@2.com",1,"umright","2018-01-25 18:17:03","umright","2018-01-25 18:17:02",0);
try {
iuser.updateUser(user);
session.commit();
} finally {
session.close();
}
}
@Test
public void testdeleteUser(){
SqlSession session = sqlSessionFactory.openSession();
IUser iuser = session.getMapper(IUser.class);
try {
iuser.deleteUser("LIUDEHUA123");
session.commit();
} finally {
session.close();
}
}
}
==============================================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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.family</groupId>
<artifactId>pengcheng</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>pengcheng Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<mybatis.version>3.3.1</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- servlet的jar包 添加<scope>provided</scope>, 因为provided表明该包只在编译和测试的时候用-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>pengcheng</finalName>
</build>
</project>