MyBatis
database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://ip:3306/database?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
user=root
password=root
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>
<properties resource="database.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="实体类包"></package>
</typeAliases>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/*.xml"></mapper>
</mappers>
</configuration>
Dao.xml
动态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="nj.zb.kb15.dao.RoleDao">
<insert id="">
insert into table_name
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="condition">
</if>
</trim>
values
<foreach collection="" item="" separator=",">
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="condition">
#{}
</if>
</trim>
</foreach>
</insert>
<update id="">
update table_name
<set>
<if test="condition">
=#{}
</id>
</set>
where =#{}
</update>
<update id="">
<foreach collection="" item="">
update table_name
<set>
<if test="condition">
=#{}
</id>
</set>
where =#{}
</foreach>
</update>
<delete id="">
delete from mrole where id in
<foreach collection="" item="" open="(" close=")" separator=",">
#{}
</foreach>
</delete>
<sql id="">
SQL语句
</sql>
<resultMap id="" type="">
<id property="" column=""></id>
<result property="" column=""></result>
<collection property="" ofType=""> //一对多
<id property="" column=""></id>
<result property="" column=""></result>
</collection>
<association property="" javaType=""> //一对一
<id property="" column=""></id>
<result property="" column=""></result>
</association>
</resultMap>
<select id="" resultMap="resultMap"/resultType="">
<include refid="sql"></include>
<where>
<if test="condition">
</if>
</where>
<choose>
<when test="condition">
</when>
<otherwise>
</otherwise>
</choose>
</select>
</mapper>
Test类
public class Test{
SqlSessionFactory sessionFactory=null;
SqlSession sqlSession=null;
Dao dao=null;
@Before
public void start(){
InputStream is = TestRole.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession=sessionFactory.openSession();
dao=sqlSession.getMapper(Dao.class);
}
@Test
public void test(){
}
@After
public void end(){
sqlSession.commit();
sqlSession.close();
System.out.println("over");
}
}