目录结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MyBatisDao</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<!--junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--日志包,方便查看动态sql语句 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>
DeptDao
public interface DeptDao {
List<Dept> SelectAllDept() throws Exception;
int InsertDept(Dept dept) throws Exception;
int UpdateDept(Dept dept) throws Exception;
int DelDept(int id) throws Exception;
}
DeptDaoImpl
public class DeptDaoImpl implements DeptDao {
public SqlSessionFactory factory;
public DeptDaoImpl(SqlSessionFactory factory) {
this.factory = factory;
}
public List<Dept> SelectAllDept() throws Exception {
List<Dept> list = null;
SqlSession session = factory.openSession();
list = session.selectList("selectAllDept");
session.close();
return list;
}
public int InsertDept(Dept dept) throws Exception {
SqlSession session = factory.openSession();
int insertDept = session.insert("insertDept", dept);
session.commit();
session.close();
return insertDept;
}
public int UpdateDept(Dept dept) throws Exception {
SqlSession session = factory.openSession();
int updateDept = session.update("updateDept", dept);
session.commit();
session.close();
return updateDept;
}
public int DelDept(int id) throws Exception {
SqlSession session = factory.openSession();
int delDept = session.delete("delDept", id);
session.commit();
session.close();
return delDept;
}
}
Dept
public class Dept {
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Dept() {
}
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
}
initLogRecord
public class initLogRecord {
public static void initLog() {
FileInputStream fileInputStream = null;
try {
Properties properties = new Properties();
fileInputStream = new FileInputStream("src/main/resources/log4j.properites");
properties.load(fileInputStream);
PropertyConfigurator.configure(properties);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
DeptDao.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="org.mybatis.example.BlogMapper">
<select id="selectAllDept" resultType="dept">
select * from dept
</select>
<insert id="insertDept" parameterType="dept">
insert into dept(deptno,dname,loc) values(#{deptno},#{dname},#{loc})
</insert>
<delete id="delDept" parameterType="int">
delete from dept where deptno = #{deptno}
</delete>
<update id="updateDept" parameterType="dept">
update dept set dname = #{dname} where deptno = #{deptno}
</update>
</mapper>
JDBC.properites
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.220.130:3306/test_project?useUnicode=true&characterEncoding=UTF-8
username=root
password=qwert123
log4j.properites
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
SqlMapConfig.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>
<properties resource="JDBC.properites"/>
<typeAliases>
<!--设置别名,name为当前实体类的所在位置,映射文件resultType别名为当前类名首字母小写后的名字-->
<package name="com.msb.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url"
value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/msb/Dao/DeptDao.xml"/>
</mappers>
</configuration>
MybatisTest
public class MybatisTest {
private SqlSessionFactory factory;
@Before
public void init() throws IOException {
initLogRecord.initLog();
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
factory = builder.build(resource);
}
@Test
public void selectAllDeptTest() throws Exception {
DeptDaoImpl deptDao = new DeptDaoImpl(factory);
List<Dept> list = deptDao.SelectAllDept();
for (Dept d : list) {
System.out.println(d.getDeptno() + ";" + d.getDname() + ";" + d.getLoc());
}
}
@Test
public void insertDeptTest() throws Exception {
DeptDaoImpl deptDao = new DeptDaoImpl(factory);
Dept d = new Dept();
d.setDeptno(60);
d.setDname("游戏部");
d.setLoc("东京");
int i = deptDao.InsertDept(d);
System.out.println(i == 1 ? "插入成功" : "插入失败");
}
}
运行结果