Mybatis
Tags:框架
[TOC]
配置文件
mybatis-config.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>
<!-- 为实体类设置别名 -->
<typeAliases>
<typeAlias type="com.zzsong.entity.pojo.Comment" alias="Comment" />
<typeAlias type="com.zzsong.entity.pojo.News" alias="News" />
<typeAlias type="com.zzsong.entity.pojo.User" alias="User" />
</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="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 导入mapper -->
<mappers>
<mapper resource="com/zzsong/mapper/ClzMapper.xml" />
</mappers>
</configuration>
JDBC.properties配置
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/getjob?characterEncoding=utf-8
user=root
password=root
mapper
<?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">
<!--等价于dao接口的实现:namespace为该xml实现的接口 -->
<mapper namespace="com.zzsong.dao.ClzDao">
<!-- 如果表的列名和类的属性名不一致:设置resultMap -->
<resultMap type="com.zzsong.entity.Clz" id="StuResMap">
<id column="id" property="cid" />
<result column="clzname" property="clzname" />
<result column="clzaddress" property="clzaddress" />
<result column="starttime" property="cstarttime" />
<result column="endtime" property="cendtime" />
<!-- 表的一对一关系:如果实体类内有对象属性,如班级中有一个老师 -->
<!-- property:班级类中教师对象的属性名 ; javaType教室对象所属的类 -->
<association property="teacher" javaType="com.zzsong.entity.Teacher">
<!-- column:数据库中表的列名 ; property:教师类中的属性名 -->
<id column="tid" property="id" />
<result column="tmane" property="name" />
</association>
<!-- 表的一对多关系:如果实体类中存在对象List,如班级中存在学生列表 -->
<collection property="list" ofType="com.iflysse.model.StuInfo">
<id column="sid" property="id" />
<result column="sname" property="name" />
<result column="classid" property="classid" />
</collection>
</resultMap>
<!-- 若设置了resultMap,则使用resultMap=“”,否则使用resultType="" -->
<!-- resultType内可填写实体类的完全限定名或者使用住配置文件中设置的该类的别名 -->
<!-- resultMap内则填写该resultMap的id -->
<select id="getClzById" parameterType="int" resultMap="StuResMap">
select * from clz where id = #{id}
</select>
<select id="getAllClz" resultType="com.zzsong.entity.Clz">
select * from clz
</select>
<insert id="addClz" parameterType="com.zzsong.entity.Clz">
insert into clz
(clzname,clzaddress,starttime,endtime) values
(#{clzname},#{clzaddress},#{starttime},#{endtime})
</insert>
<update id="updateClz" parameterType="com.zzsong.entity.Clz">
update clz set
clzname=#{clzname},clzaddress=#{clzaddress},starttime=#{starttime},endtime=#{endtime}
where id = #{id}
</update>
<delete id="deleteClzById" parameterType="int">
delete from clz where
id = #{id}
</delete>
<!-- 动态SQL -->
<select id="getClzes" parameterType="java.util.Map" resultMap="StuResMap">
select * from clz where
<if test="clzname != '%null%'">
clzname like #{clzname} and
</if>
<if test="clzaddress != '%null%'">
clzaddress like #{clzaddress}
</if>
</select>
</mapper>
调用存储过程
新建存储过程
CREATE PROCEDURE getUserCountBySex(in sex_index int,OUT myCount int)
BEGIN
IF(sex_index = 0)
THEN
select COUNT(1) INTO myCount from userinfo t where t.sex = '女';
ELSE
select COUNT(1) INTO myCount from userinfo t where t.sex = '男';
end IF;
END
SET @myCount = 0;
CALL getUserCountBySex(1,@myCount);
SELECT @myCount;
Mybatis调用存储过程
<!-- 调用存储过程 -->
<select id="getNumBySex" parameterMap="myMap" statementType="CALLABLE">
call getNumBySex(?,?)
</select>
<parameterMap type="java.util.Map" id="myMap">
<parameter property="sexIndex" mode="IN" jdbcType="INTEGER" />
<parameter property="sexCount" mode="OUT" jdbcType="INTEGER" />
</parameterMap>
调用
普通调用
public class MainTest {
public static void main(String[] args) {
InputStream ins = MainTest.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(ins);
SqlSession session = factory.openSession(true);
String statement = "com.zzsong.mapper.ClzMapper.getClzes";
String clzname = null;
String clzaddress = "1";
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("clzname", '%' + clzname + '%');
map.put("clzaddress", '%' + clzaddress + '%');
System.out.println(session.selectList(statement, map));
}
}
实现接口
main
public class MainText {
public static void main(String[] args) {
InputStream ins = MainText.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(ins);
SqlSession session = factory.openSession();
ClassDao classDao = session.getMapper(ClassDao.class);
}
}
Factory
public class DaoFactory {
private static SqlSession session;
static {
InputStream ins = DaoFactory.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(ins);
session = factory.openSession(true);
}
public static UserDao getUserDao() {
return session.getMapper(UserDao.class);
}
public static NewsDao getNewsDao() {
return session.getMapper(NewsDao.class);
}
public static CommentDao getCommentDao() {
return session.getMapper(CommentDao.class);
}
}