使用接口代理是需要注意:
1.xml和java接口必须同包同名
2.主xml文件中<mapper>标签中‘class’属性需要映射到接口
3.被映射的xml(和接口同名)中<mapper namespace="接口全路径">
4.操作标签(增删改查)的id和接口中方法名一致
5.接口不需要实现,SqlSessionFactory获取SqlSession对象,再通过反射sqlSession.getMapper(接口名.class) 得到对象,调用接口方法;
InputStream is = Resources.getResourceAsStream("com/bwf01/config/main.xml");//读主配置文件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
接口 jm = sqlSession.getMapper(接口.class);//jm 调用直接接口中的方法
主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="com/bwf01/config/sqlConfig.properties">
<property name="driver" value="driver"/>
<property name="url" value="url"/>
<property name="username" value="user"/>
<property name="password" value="password"/>
</properties>
<!-- 设置映射别名 别名是为 Java 类型设置一个短的名字
<typeAliases>
<typeAlias type="" alias=""/>
</typeAliases> -->
<settings><!-- 配置日志 -->
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 设置映射 和接口同名的xml文件-->
<mappers>
<mapper class="com.bwf01.mapper.JobMapper"></mapper>
<mapper class="com.bwf01.mapper.UserMapper"></mapper>
<mapper class="com.bwf01.mapper.FirmMapper"></mapper>
<mapper class="com.bwf01.mapper.AdminMapper"></mapper>
<mapper class="com.bwf01.mapper.PVMapper"></mapper>
</mappers>
</configuration>
实现接口代理的xml文件:
1.操作标签(增删改查)的id和接口中方法名一致
2.数据库字段和类属性的映射对应
3.配置字段和属性映射时可以使用 extends
4.操作标签中可以用动态sql语句
<?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">
<mapper namespace="com.bwf01.mapper.JobMapper">
<resultMap type="com.bwf01.bean.Job" id="shareselectJob">
<!--数据库字段和类属性的映射对应 -->
<id column="jobId" property="jobId"/>
<result column="jobName" property="jobName"/>
<result column="workPlace" property="workPlace"/>
<result column="workType" property="workType"/>
<result column="numbers" property="numbers"/>
<result column="salary" property="salary"/>
<result column="payType" property="payType"/>
<result column="workDate" property="workDate"/>
<result column="workTime" property="workTime"/>
<result column="endDate" property="endDate"/>
<result column="firmId" property="firmId"/>
<result column="principal" property="principal"/>
<result column="tell" property="tell"/>
<result column="workDetails" property="workDetails"/>
<result column="status" property="status"/>
<result column="curentNum" property="curentNum"/>
<result column="joinStatus" property="joinStatus"/>
</resultMap>
<resultMap type="com.bwf01.bean.Job" id="selectJob" extends="shareselectJob">
<!-- 某个属性是自定义类型,联表查询映射到一个表中的对应字段 -->
<association property="firm" javaType="com.bwf01.bean.Firm">
<id column="firmId" property="firmId"/>
<result column="logo" property="logo"/>
<result column="firmName" property="firmName"/>
</association>
<!-- 某个属性是自定义类型,联表查询映射到一个表中的对应字段 -->
<association property="jobKind" javaType="com.bwf01.bean.JobKind">
<id column="kindId" property="kindId"/>
<result column="jobStyle" property="jobStyle"/>
</association>
</resultMap>
<select id="selectJobByPage" parameterType="com.bwf01.vo.JobPage" resultMap="selectJob">
select job.*,logo,firmName,jobStyle from job INNER JOIN firm join jobkind
on job.firmId = firm.firmId and jobkind.kindId = job.kindId
where job.status = '审核通过' and joinStatus = '正在报名'
<if test="kindId !=0">
and job.kindId = #{kindId}
</if>
<if test="region!=null and region!=''">
and job.workPlace like '%${region}%'
</if>
limit ${(page-1)*10},10
</select>
</mapper>