Mybatis入门-原始Dao层开发(四)

目录结构
在这里插入图片描述
在这里插入图片描述


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&amp;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 ? "插入成功" : "插入失败");
}
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值