加载sqlMapConfig.xml——>创建SqlSessionFactory——>创建SqlSession——>SqlSEssion帮忙生成一个实现类(给接口)——>调用findUserById得到查询返回的pojo对象
MybatisMapperTest
package com.itheima.mybatis.junit;
import static org.junit.Assert.*;
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;
import org.junit.Test;
import com.itheima.mybatis.mapper.UserMapper;
import com.itheima.mybatis.pojo.User;
public class MybatisMapperTest {
@Test
public void testMapper() throws Exception {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//SqlSEssion帮我生成一个实现类 (给接口)
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(10);
System.out.println(user);
}
}
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="jdbc.properties"/>
<!-- 别名 包以其子包下所有类 头字母大小都行-->
<typeAliases>
<!-- <typeAlias type="com.itheima.mybatis.pojo.User" alias="User"/> -->
<package name="com.itheima.mybatis.pojo"/>
</typeAliases>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="nideshengri" />
</dataSource>
</environment>
</environments>
<!-- Mapper的位置 Mapper.xml 写Sql语句的文件的位置 -->
<mappers>
<!-- <mapper resource="sqlmap/User.xml" class="" url=""/> -->
<!-- <mapper resource="sqlmap/User.xml" class="" url=""/> -->
<!-- <mapper class="com.itheima.mybatis.mapper.UserMapper" /> -->
<!-- <mapper url="" /> -->
<package name="com.itheima.mybatis.mapper"/>
</mappers>
</configuration>
UserMapper
package com.itheima.mybatis.mapper;
import com.itheima.mybatis.pojo.User;
public interface UserMapper {
//遵循四个原则
//接口 方法名 == User.xml 中 id 名
//返回值类型 与 Mapper.xml文件中返回值类型要一致
//方法的入参类型 与Mapper.xml中入参的类型要一致
//命名空间 绑定此接口
public User findUserById(Integer id);
}
UserMapper.xml
<?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">
<!-- 写Sql语句 -->
<mapper namespace="com.itheima.mybatis.mapper.UserMapper">
<!-- 通过ID查询一个用户 -->
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user where id = #{v}
</select>
<!-- //根据用户名称模糊查询用户列表
#{} select * from user where id = ? 占位符 ? == '五'
${} select * from user where username like '%五%' 字符串拼接
-->
<select id="findUserByUsername" parameterType="String" resultType="com.itheima.mybatis.pojo.User">
select * from user where username like "%"#{haha}"%"
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.itheima.mybatis.pojo.User">
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user (username,birthday,address,sex)
values (#{username},#{birthday},#{address},#{sex})
</insert>
<!-- 更新 -->
<update id="updateUserById" parameterType="com.itheima.mybatis.pojo.User">
update user
set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address}
where id = #{id}
</update>
<!-- 删除 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user
where id = #{vvvvv}
</delete>
</mapper>