MyBatis框架
数据库表
CREATE DATABASE ssm;
USE ssm;
CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(100),
money DOUBLE(4,2)
);
INSERT INTO account VALUES (NULL,'cat',9.16),(NULL,'dog',11.27);
结构目录
java
resources
SqlMapConfig.xml (数据库连接配置文件)
数据库连接配置文件
方式一
jdbcConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ee314
jdbc.username=root
jdbc.password=1234
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 = " jdbcConfig.properties" > </ properties>
< environments default = " mysql" >
< environment id = " mysql" >
< transactionManager type = " JDBC" > </ transactionManager>
< dataSource type = " pooled" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< package name = " cn.water.dao" />
</ mappers>
</ configuration>
方式二
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>
< 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:///ssm" />
< property name = " username" value = " root" />
< property name = " password" value = " root" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< package name = " cn.water.dao" />
</ mappers>
</ configuration>
持久层
AccountDao
package cn. water. dao;
import cn. water. domain. Account;
import org. apache. ibatis. annotations. Insert;
import org. apache. ibatis. annotations. Select;
import java. util. List;
public interface AccountDao {
@Select ( "select * from account" )
public abstract List< Account> findAll ( ) ;
@Insert ( "insert into account (name,money) values (#{name},#{money})" )
public abstract void add ( Account account) ;
}
测试类
TestMyBatis
package cn. water. test;
import cn. water. dao. AccountDao;
import cn. water. domain. Account;
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. junit. Test;
import java. io. IOException;
import java. io. InputStream;
import java. util. List;
public class TestMyBatis {
@Test
public void run01 ( ) throws Exception {
InputStream inputStream = Resources. getResourceAsStream ( "SqlMapConfig.xml" ) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
AccountDao mapper = sqlSession. getMapper ( AccountDao. class ) ;
List< Account> all = mapper. findAll ( ) ;
for ( Account account : all) {
System. out. println ( account) ;
}
sqlSession. close ( ) ;
inputStream. close ( ) ;
}
}
运行结果
Account{id=1, name='cat', money=9.16}
Account{id=2, name='dog', money=11.27}