1.引入依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--持久层结束-->
2.编写持久层代码
public interface AccountDao {
/*查询全部*/
@Select("select * from account")
public List<Account> findAll();
/*根据id查*/
@Select("select * from account where id=#{id}")
public Account findById(Integer id);
/*增加*/
@Insert("insert into account values(null,#{name},#{money})")
public void save(Account account);
/*删除*/
@Delete("delete from account where id=#{id}")
public void delete(Integer id);
/*修改*/
@Update("update account set name=#{name},money=#{money} where id=#{id}")
public void update(Account account);
}
3.编写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>
<!--对应applicationContext中引入外部数据库相关属性配置文件-->
<properties resource="jdbc.properties"></properties>
<!--对应applicationContext中配置方法二-->
<typeAliases>
<!--配置别名,引用时只需要写简单类名,不需要全类名-->
<package name="com.it.domain"></package>
</typeAliases>
<!--对应applicationContext中创建sqlSessionFactory对象-->
<!--数据库配置文件-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<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>
<!--对应applicationContext中的扫描dao层接口的包,创建动态代理对象-->
<mappers>
<!--映射该包下的全部映射文件-->
<package name="com.it.dao"></package>
</mappers>
</configuration>
4.引入数据库配置jdbc.properties属性文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shop
jdbc.username=root
jdbc.password=root
5.测试持久层数据
@Test
public void testMybatis(){
//获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(
this.getClass().getClassLoader().getResourceAsStream("sqlMapConfig.xml"));
//获取sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
List<Account> accountList = accountDao.findAll();
for (Account account : accountList) {
System.out.println(account);
}
}