<dependencies>
<!-- mybatis框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- 连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!-- 测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
package com.cenzn.domain;
public class Account {
private Integer id;
private String name;
private Float money;
//Setter
//Getter
//toString
}
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@ResultMap:实现引用@Results 定义的封装
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
@SelectProvider: 实现动态 SQL 映射
@CacheNamespace:实现注解二级缓存的使用
package com.cenzn.dao;
import com.cenzn.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface IAccountDao {
/**
* 查询所有账户
* @return
*/
@Select("select * from account")
public List<Account> findAccountAll();
/**
* 保存账户
* @param account
*/
@Insert("insert into account(name,money)values(#{name},#{money})")
public void saveAccount(Account account);
/**
* 更新账户
* @param account
*/
@Update("update account set name = #{name},money=#{money} where id =#{id}")
public void updateAccount(Account account);
/**
* 删除账户
* @param accountId
*/
@Select("delete from account where id = #{accountId}")
public void deleteAccout(Integer accountId);
}
<configuration>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
dataSource>
environment>
environments>
<mappers>
<mapper class="com.cenzn.dao.IAccountDao"/>
mappers>
configuration>
private InputStream in;
private SqlSessionFactoryBuilder builder;
private SqlSessionFactory factory;
private SqlSession session;
private IAccountDao accountDao;
//方法开始之前执行
@Before
public void init() throws IOException {
System.out.println("================init================");
//1.读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建 SqlSessionFactory 的构建者对象
builder = new SqlSessionFactoryBuilder();
//3.使用构建者创建工厂对象 SqlSessionFactory
factory = builder.build(in);
//4.使用 SqlSessionFactory 生产 SqlSession 对象
session = factory.openSession(true);//true自动提交事务
//5.使用 SqlSession 创建 dao 接口的代理对象
accountDao = session.getMapper(IAccountDao.class);
}
//方法结束之后执行
@After
public void destroy() throws IOException {
System.out.println("================destroy================");
in.close();
session.close();
}
@Test
public void findTest(){
List<Account> accounts = accountDao.findAccountAll();
for (Account account : accounts){
System.out.println(account);
}
}
@Test
public void saveTest(){
Account account = new Account();
account.setName("ddd");
account.setMoney(1234f);
accountDao.saveAccount(account);
}
@Test
public void UpdateTest(){
Account account = new Account();
account.setId(4);
account.setName("ddd");
account.setMoney(12345f);
accountDao.updateAccount(account);
}
@Test
public void DeleteTest(){
accountDao.deleteAccout(4);
}