MyBatis配置类、动态SQL及测试类格式

这篇博客详细介绍了MyBatis的配置过程,包括database.properties文件中的数据库连接信息,mybatis-config.xml中的全局设置、类型别名、数据源配置以及Mapper XML文件中的SQL动态语句。同时展示了Test类中如何初始化SqlSessionFactory和执行测试操作。
摘要由CSDN通过智能技术生成

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");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

honconM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值