Java DB 访问之 mybatis mapper xml 配置方式

Posted hbb0b0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java DB 访问之 mybatis mapper xml 配置方式相关的知识,希望对你有一定的参考价值。

1 项目说明

项目采用 maven 组织 ,依赖 mysql-connector-java,org.mybatis,junit pom 依赖如下:

 

mysql 数据连接 :

mysql-connector-java

 

mybatis

mybatis

 

junit

junit 单元测试,本项目采用junit 跑单元测试,4.12 版本采用 @Test注解 测试方法,3.X 版本采用继承来实现测试方法

 

<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>

 

2 项目结构

 

 

 

3 DB 脚本

DROP TABLE IF EXISTS `account`;

CREATE TABLE `account` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(45) DEFAULT NULL,

`money` decimal(10,0) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8;

 

-- ----------------------------

-- Records of account

-- ----------------------------

INSERT INTO `account` VALUES (\'1\', \'hbb0b0\', \'40000\');

INSERT INTO `account` VALUES (\'2\', \'kael\', \'1000\');

 

 

4 配置与代码说明

 

  • DB 连接信息与mybatis 配置
<?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="Account" type="hbb0b0.JavaBasic.model.Account"/>

</typeAliases>

 

<environments default="development">

<environment id="development">

<transactionManager type="JDBC"/>

<dataSource type="POOLED">

<property name="driver" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/study" />

<property name="username" value="root"/>

<property name="password" value="sqlsa"/>

</dataSource>

</environment>

</environments>

<mappers>

<mapper resource="hbb0b0/JavaBasic/mapper/AccountMapper.xml"/>

</mappers>

</configuration>

 

 

  • model
package hbb0b0.JavaBasic.model;

 

import java.io.Serializable;

 

public class Account implements Serializable{

private int id;

private String name;

private double money;

public Account() {

super();

}

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 double getMoney() {

return money;

}

public void setMoney(double money) {

this.money = money;

}

@Override

public String toString() {

return "Account [id=" + id + ", name=" + name + ", money=" + money

+ "]";

}

}

 

 

  • xml mapper
<?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="hbb0b0.JavaBasic.AccountMapper">

 

<select id="getOne" resultType="hbb0b0.JavaBasic.model.Account">

select * from account limit 1,1

</select>

<select id="getList" resultType="hbb0b0.JavaBasic.model.Account">

select * from account

</select>

<insert id="insertAccount" parameterType="hbb0b0.JavaBasic.model.Account">

insert into account (name,money)

values (#{name},#{money})

</insert>

 

<delete id="deleteAccount" parameterType="java.lang.String">

delete from account where name like \'${name}%\'

</delete>

</mapper>

 

  • session简单封装
package hbb0b0.JavaBasic.dbTool;

 

import java.io.IOException;

import java.io.InputStream;

 

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

 

public class DBTool {

 

public static SqlSession getSession() throws IOException

{

String resource = "Configuration.xml";

InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory =

new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sqlSessionFactory.openSession();

return session;

}

}

 

 

  • JUNit
package hbb0b0.JavaBasic.mybatis;

 

import java.io.IOException;

import java.util.Date;

import java.util.List;

 

import org.apache.ibatis.session.SqlSession;

 

import hbb0b0.JavaBasic.dbTool.DBTool;

import hbb0b0.JavaBasic.model.Account;

 

import org.junit.*;

 

import static org.junit.Assert.*;

 

/**

* Unit test for simple App.

*/

public class AppTest {

 

@Test

public void getOne_Test() throws IOException {

SqlSession session = DBTool.getSession();

try {

 

Account account = session.selectOne("hbb0b0.JavaBasic.AccountMapper.getOne");

 

assertNotNull(account);

 

} finally {

session.close();

}

}

 

@Test

public void getList_Test() throws IOException {

SqlSession session = DBTool.getSession();

try {

 

List<Account> accountList = session

.selectList("hbb0b0.JavaBasic.AccountMapper.getList");

 

assertNotNull(accountList);

 

Assert.assertTrue(accountList.size() == 102);

} finally {

session.close();

}

}

 

@Test

public void insert_Test() throws IOException {

SqlSession session = DBTool.getSession();

try {

 

Account account = new Account();

account.setName("mybatisInsert-" + new Date().toString());

account.setMoney(10000);

int result = session.insert(

"hbb0b0.JavaBasic.AccountMapper.insertAccount", account);

session.commit();

Assert.assertTrue(result > 0);

} finally {

session.close();

}

}

 

@After

public void SetUp() throws IOException {

SqlSession session = DBTool.getSession();

try {

 

Account account = new Account();

account.setName("mybatisInsert");

int result = session.delete(

"hbb0b0.JavaBasic.AccountMapper.deleteAccount",account);

session.commit();

//Assert.assertTrue(result > 0);

} finally {

session.close();

}

}

 

}

 

 

测试结果:

 

 

 

 

 

 

以上是关于Java DB 访问之 mybatis mapper xml 配置方式的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis之拒绝重复代码

Mybatis学习XML配置文件之mybatis-config.xml

Java DB访问之 JDBC

攻占SSM之手写Mybatis的xml实现Dao层接口

#yyds干货盘点# 深入浅出 Spring Boot - 数据访问之 MyBatis

mybatis 之 DB的频繁修改应对策略