Before use mybatis framework reading work:
jar package
core config XML file
mapper XML file
Core config xml file include:
- properties file input.
- class path alias setting.
- mapper file and mapper class register.
- dataSource information config.
mapper xml file include:
namespace token mapper file unique.
config execute sql command(insert,delete,update,select).
获取Sqlsession对象:
String resource = "conf.xml";
//加载 mybatis 的配置文件(它也加载关联的映射文件)
InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
//构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中 sql 的 sqlSession
SqlSession session = sessionFactory.openSession();
mybatis配置文件参数详解:
- mybatis配置文件引人properties文件:
<properties resource="db.properties" />
- 因为配置文件中类路径过长,这里可以通过别名缩短类路径:
<!-- 两种方式设置 -->
<typeAlias type="com.haw.User" alias="User" />
<package name="com.been" /><!-- 需要注意id标示为类名,且第一个字母小写 -->
- 数据源配置:
<!-- 定义模式 1.work 2.development -->
<environments default="development">
<environment id="development">
<!-- 事务管理交给jdbc ,这里可以交给spring容器管理 -->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- 数据库配置 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
4.映射文件注册:
<!-- 映射文件注册-->
<mappers>
<!-- 通过xml方式注册 -->
<mapper resource="com/haw/userMapper.xml" />
<mapper resource="com/haw2/userMapper.xml" />
<mapper resource="com/haw4/userMapper.xml" />
<mapper resource="com/haw5/classesMapper.xml" />
<mapper resource="com/haw6/studentMapper.xml" />
<mapper resource="com/haw7/userMapper.xml" />
<!-- 通过类注册 -->
<mapper class="com.haw3.UserMapper" />
</mappers>
mybatis映射文件参数详解:
- 每个映射文件必须声明一个命名空间(namespace必须是唯一的):
<mapper namespace="com.haw.userMapper">
</mapper>
- 一个简单的增删改查配置:
<insert id="insertUser" parameterType="User">
insert into Users(name,age)values(#{name},#{age})
</insert>
<delete id="deleteUser" parameterType="int">
delete from Users where id = #{id}
</delete>
<update id="updateUser" parameterType="User">
update Users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getUserAll" resultType="User">
select * from Users
</select>
<select id="getUser" parameterType="int" resultType="User" >
select * from Users where id = #{id}
</select>
- id:操作的唯一标识
- parameter:传入参数。
- resultType:返回参数。
- 解决数据库字段名与实体类属性名不相同的冲突
<select id="getOrder" parameterType="int" resultMap="OrderMapper">
select * from orders where order_id = #{id}
</select>
<resultMap type="Order" id="OrderMapper">
<id property="id" column="order_id" />
<result property="orderNo" column="order_no" />
<result property="price" column="order_price" />
</resultMap>
通过resultMap参数配置数据库字段雨实体类映射关系。
<id property="id" column="order_id" />
这里的id标签对应数据库的id字段。
property实体类字段。
column数据库字段。
- 一对一查询方式:
<!-- 一对一查询方式 -->
<select id="getStudent" parameterType="int" resultMap="studentMap">
select *
from class c
right join teacher t
on c.teacher_id = t.t_id
right join student s
on c.c_id = s.class_id
where c.c_id = #{id}
</select>
<resultMap type="Classes" id="studentMap">
<id property="id" column="c_id" />
<result property="name" column="c_name" />
<!-- 这层property="teacher" 为classes类中的属性teacher column 用来射影数据库中的字段 javaType对应Teacher类 -->
<!-- association用于单行数据处理 -->
<association property="teacher" column="teacher_id"
javaType="Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name" />
</association>
<!-- collection用于多行数据处理比如List -->
<collection property="list" ofType="Student">
<id property="id" column="s_id" />
<result property="name" column="s_name" />
</collection>
</resultMap>
- 通过注解方式配置映射关系:
实现过程:创建接口,配置信息通过注解方式配置。实现类实现接口。
// 通过注解方式
public interface UserMapper {
@Insert("insert into users(name,age)values(#{name},#{age})")
public int insert(User user);
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int update(User user);
@Delete("delete from users where id = #{id}")
public int delete(int id);
@Select("select * from users where id = #{id}")
public User getUser(int id);
@Select("select * from users")
public List<User> getAllUser();
}
@Test
public void testInsert() {
SqlSessionFactory sessionFactory = GetSession.seesion();
SqlSession session = sessionFactory.openSession(true);
UserMapper userMapper = session.getMapper(UserMapper.class);
int add =userMapper.insert(new User(-1,"hahah",22));
session.close();
System.out.println(add);
}
@Test
public void testUpdate() {
SqlSessionFactory sessionFactory = GetSession.seesion();
SqlSession session = sessionFactory.openSession(true);
UserMapper userMapper = session.getMapper(UserMapper.class);
int update =userMapper.update(new User(6,"user",0));
session.close();
System.out.println(update);
}
@Test
public void testDelete(){
SqlSessionFactory sessionFactory = GetSession.seesion();
SqlSession session = sessionFactory.openSession(true);
UserMapper userMapper = session.getMapper(UserMapper.class);
int delete =userMapper.delete(4);
session.close();
System.out.println(delete);
}
@Test
public void testGetUser(){
SqlSessionFactory sessionFactory = GetSession.seesion();
SqlSession session = sessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
User user =userMapper.getUser(6);
session.close();
System.out.println(user);
}
@Test
public void testGetAllUser() {
SqlSessionFactory sessionFactory = GetSession.seesion();
SqlSession session = sessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
List<User> user =userMapper.getAllUser();
session.close();
System.out.println(user);
}