侧边栏壁纸
  • 累计撰写 793 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

目 录CONTENT

文章目录

原始使用方式

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 148 阅读 / 1,087 字
温馨提示:
本文最后更新于 2022-07-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

dao层只定义接口,不用自己实现,用这里映射过去。
namespace 写成接口名,mapping的id写成接口里的方法名。
使用
//下面这句可以用单例模式
public SqlSession sqlSession = (new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("sqlMapConfig.xml"))).openSession();
MyDao mapper = sqlSession.getMapper(MyDao.class);

System.out.println("--------------insert---------------");
MyNew insertNew = new MyNew("insert", "123", "testInsert", new Date());
System.out.println(mapper.insert(insertNew));
System.out.println("insertNew.getId() = " + insertNew.getId());
sqlSession.commit();

System.out.println("--------------getAll---------------");
List<MyNew> res = mapper.getAll();
res.forEach(System.out::println);

System.out.println("--------------getOneById---------------");
System.out.println((mapper.getOneById(19)).toString());

System.out.println("--------------getAllByLikeCase---------------");
(mapper.getAllByLikeCase("i")).forEach(System.out::println);

System.out.println("--------------update---------------");
MyNew updateNew = new MyNew("update", "123", "testUpdate", new Date());
updateNew.setId(19);
System.out.println(mapper.update(updateNew));
sqlSession.commit();

System.out.println("--------------delete---------------");
System.out.println(mapper.delete("insert"));
sqlSession.commit();
主配置文件
<?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="db.properties"> </properties>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/> <!--往console里输出日志-->
				<!--  开启懒加载  -->
				<setting name="lazyLoadingEnabled" value="true"/>
				<setting name="aggressiveLazyLoading" value="false"/>
				<!-- 开启二级缓存 -->
				<setting name="cacheEnabled" value="true"/>
    </settings>

    <typeAliases><!--起别名-->
<!--        <typeAlias alias="Post" type="com.ljj.entity.Post"/>-->
        <package name="com.ljj.entity"/> 
        <package name="com.ljj.dao"/>
    </typeAliases>

    <environments default="ljj">
        <environment id="ljj">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED"><!--使用集成的连接池-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="JDBC:mysql://localhost:3306/bbs"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
<!--                <package name="com.ljj.dao"/>-->
        <mapper resource="com/ljj/dao/ReplyMapper.xml"/>
<!--        <mapper class="com.ljj.dao.IReplyDao"/>-->
    </mappers>

</configuration>
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">


<mapper namespace="com.ljj.dao.IReplyDao"><!--一定要和接口类名一致-->
<!--下面的id和接口里的方法名一致-->
    <resultMap id="getAllResultMapper" type="Post">
        <id column="pId" property="id"/>
        <result column="pTitle" property="title"/>
        <result column="pContext" property="context"/>
        <result column="pTime" property="time"/>
        <result column="pUId" property="userId"/>
        <result column="pUName" property="userName"/>
        <collection property="replies" javaType="Reply">
            <result column="rId" property="id"/>
            <result column="rContext" property="context"/>
            <result column="rTime" property="time"/>
            <result column="rUId" property="userId"/>
            <result column="rUName" property="userName"/>
        </collection>
    </resultMap>
    <select id="getAll" parameterType="int" resultMap="getAllResultMapper">
SELECT
	pId,
	pTitle,
	pContext,
	pTime,
	post.uId AS pUId,
	pUser.uName AS pUName,
	rId,
	rContext,
	rTime,
	reply.uId AS rUId,
	rUser.uName AS rUName
FROM
	post
	JOIN USER AS pUser ON post.uId = pUser.uId
	JOIN reply USING ( pId )
	JOIN USER AS rUser ON reply.uId = rUser.uId
WHERE
	pId = #{pId}
ORDER BY
	rTime
	 </select>

    <insert id="add" parameterType="Reply"><!--这里员Dao返回的是boolean-->
        <selectKey resultType="int" keyProperty="id" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into reply values(null,#{context},#{time},#{userId},#{postId})
    </insert>

    <select id="getIdByTitle" parameterType="String" resultType="int">
        select rId from reply where rTitle=#{title}
    </select>
    <delete id="delete" parameterType="Reply">
        <!--    public boolean delete(int replyId, int postId, int userId, boolean isAdmin);-->
        delete from reply where pId=#{postId} and rId=#{id}
    </delete>
    <update id="change" parameterType="Reply">
        <!--    public boolean change(int replyId, int postId, int userId, String content);-->
        update reply set rContext=#{context} where pId=#{postId} and rId=#{id}
    </update>

    <select id="getUserId" parameterType="int" resultType="int">
        select uId from reply where rId=#{rId}
    </select>


</mapper>
接口定义
public interface MyDao {
    public List<MyNew> getAll();
    public MyNew getOneById(int id);
    public List<MyNew> getAllByLikeCase(String value);
    public int insert(MyNew obj);
    public int update(MyNew obj);
    public int delete(String title);
}
0

评论区