项目目录
1.两个配置文件
全局配置文件mybatis-config.xml
在配置数据源时将数据库配置写在jdbc.properties中
jdbc.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/testdemo
username=root
password=root
mybatis-config.xml
五个部分
1.读取jdbc.properties资源文件
2.配置jdbc数据源
3.mapper.xml配置文件路径,告诉mybatis去哪里找sql语句
4.如果数据库字段和实体类字段不同,且数据库字段符合驼峰命名格式则可开启驼峰命名转换
5.在mapper.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>
<!-- 加载jdbc的配置文件 -->
<properties resource="jdbc.properties"></properties>
<!-- 驼峰命名转换 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<!-- 为java类型命名的一个短名字在mapperxml里面可以只用类名 -->
<typeAliases>
<typeAlias alias="Role" type="com.hwj.pojo.Role" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driverClass}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hwj/mapper/roleMapper.xml" />
</mappers>
</configuration>
mapper.xml映射文件
dao的实现类对sqlsession的使用方式很相似,mybatis接口实现了动态代理,不需要接口的实现类,
namespace的值需要和对应dao接口的全路径一致,dao层接口名称与sql定义的id一致,但本身如果有dao接口的实现类对namespace的定义是没有限制的
接口没有实现类所以需要特殊的实例化,通过sqlsession.getMapper(dao.class)获得
接口代码:
package com.hwj.dao;
import java.util.List;
import com.hwj.pojo.Role;
public interface IRole {
public List<Role> selectAll();
public Role selectById(Long id);
public int delete(Long id);
public int update(Role role);
public int add(Role role);
}
mapper.xml代码
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hwj.dao.IRole">
<select id="selectAll" resultType="Role">
select * from role;
</select>
<select id="selectById" resultType="Role">
select * from role where id=#{id}
</select>
<insert id="add" parameterType="Role">
insert into role(role_name,description) values(#{roleName},#{description})
</insert>
<delete id="delete">
delete from role where id=#{id}
</delete>
<update id="update" parameterType="Role">
update role set role_name=#{roleName},description=#{description} where id=#{id}
</update>
</mapper>
#{} 存在mapper.xml中的sql语句部分,标识该位置可以接受参数信息。相当于?占位符。
测试类:
1.从xml中构建SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
2.从sqlSessionFactory得到SqlSession
SqlSession session = sqlSessionFactory.openSession();
3.实例化dao
IRole roledao=session.getMapper(IRole.class);
4.操作dao的方法
新增部分:多表查询,延迟加载
表:role user user_role
role user
user-role
查询某个角色下所有的用户
Role类中有一个属性为List<User>多对一的关系
Role类:
package com.hwj.pojo;
import java.util.ArrayList;
import java.util.List;
public class Role {
private Long id;
private String roleName;
private String description;
private List<User> list;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
@Override
public String toString() {
return "Role [id=" + id + ", roleName=" + roleName + ", description=" + description + ", list=" + list + "]";
}
public Role(String roleName, String description) {
this.roleName = roleName;
this.description = description;
}
public Role(Long id, String roleName, String description) {
this.id = id;
this.roleName = roleName;
this.description = description;
}
public Role() {
}
}
mapper.xml中
<select id="selectT" resultType="User">
select u.* from user_role ur inner join user u on ur.user_id=u.id where ur.role_id=#{id};
</select>
<resultMap type="Role" id="result">
<id column="id" property="id"></id>
<result column="username" javaType="String" property="username"/>
<result column="password" javaType="String" property="password"/>
<result column="create_time" javaType="Date" property="createTime"/>
<collection property="list" javaType="list" ofType="User" autoMapping="true" select="selectT" column="id"></collection>
</resultMap>
<select id="selectTwo" resultMap="result">
select * from Role where id=#{id}
</select>
selectTwo接口的返回值为Role