目录
[1.1. 工程结构](#1.1. 工程结构)
[1.2. sqlMapConfig.xml](#1.2. sqlMapConfig.xml)
[1.3. properties](#1.3. properties)
[1.4. UserMapper.xml](#1.4. UserMapper.xml)
[1.5. UserMapper](#1.5. UserMapper)
[1.6. UserMapperImpl](#1.6. UserMapperImpl)
[1.7. UserService](#1.7. UserService)
[1.8. User实体类](#1.8. User实体类)
[2.1. 概述](#2.1. 概述)
[2.2. 基于传统方式上调整的代码](#2.2. 基于传统方式上调整的代码)
[2.2.1. Dao接口](#2.2.1. Dao接口)
[2.2.2. UserService](#2.2.2. UserService)
[2.2.3. UserMapper.xml](#2.2.3. UserMapper.xml)
一、传统的Dao层调用实现方式
编写Dao接口
编写接口实现
编写业务Service调用
1.1. 工程结构
1.2. sqlMapConfig.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 加载外部properties -->
<properties resource="jdbc.properties"></properties>
<typeAliases>
<typeAlias type="com.example.domain.User" alias="user"></typeAlias>
</typeAliases>
<!-- 数据源环境 -->
<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>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
1.3. properties
XML
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=Wangzhexiao
1.4. UserMapper.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper">
<!-- 查询操作 -->
<select id="findAll" resultType="user">
select * from User
</select>
</mapper>
1.5. UserMapper
java
package com.example.mapper;
import com.example.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
List<User> findAll() throws IOException;
}
1.6. UserMapperImpl
java
package com.example.mapper.impl;
import com.example.domain.User;
import com.example.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserMapperImpl implements UserMapper {
@Override
public List<User> findAll() throws IOException {
// 获得核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 获得Session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得Session会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行操作 参数:namespace+id
return sqlSession.selectList("userMapper.findAll");
}
}
1.7. UserService
java
package com.example.service;
import com.example.domain.User;
import com.example.mapper.UserMapper;
import com.example.mapper.impl.UserMapperImpl;
import java.io.IOException;
import java.util.List;
public class UserService {
public static void main(String[] args) throws IOException {
// 创建dao对象
UserMapper userMapper = new UserMapperImpl();
List<User> list = userMapper.findAll();
System.out.println(list);
}
}
1.8. User实体类
java
package com.example.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private int id;
private String username;
private String password;
}
二、代理方式(主流)
2.1. 概述
采用 Mybatis 的代理开发方式实现DAO层的开发,这种方式是我们后面进入企业的主流。
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao 接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper 接口开发需要遵循以下规范:
- Mapper.xml文件中的namespace与mapper接口的全限定名相同
- Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
- Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType类型相同
- Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
2.2. 基于传统方式上调整的代码
2.2.1. Dao接口
java
package com.example.mapper;
import com.example.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
List<User> findAll() throws IOException;
User findById(Integer id) throws IOException;
}
2.2.2. UserService
java
package com.example.service;
import com.example.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
public class UserService {
public static void main(String[] args) throws IOException {
// 获得核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 获得Session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得Session会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取Dao代理对象,Mybatis内部已经帮我们完成了实现(与我们传统方式代码中的实现类相似)
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.findAll());
System.out.println(mapper.findById(1));
}
}
2.2.3. UserMapper.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<!-- 查询操作 -->
<select id="findAll" resultType="user">
select * from User
</select>
<!-- 查询操作 -->
<select id="findById" parameterType="java.lang.Integer" resultType="user">
select * from User where id = #{id}
</select>
</mapper>