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

原始使用方式

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

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>

&lt;typeAliases&gt;&lt;!--起别名--&gt;

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

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

&lt;mappers&gt;

<!-- <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 = #

ORDER BY
rTime
</select>

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

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

&lt;select id=&quot;getUserId&quot; parameterType=&quot;int&quot; resultType=&quot;int&quot;&gt;
    select uId from reply where rId=#{rId}
&lt;/select&gt;

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

评论区