mybits学习3

 

所花时间(包括上课):

 2h

代码量(行):

 150左右

搏客量(篇):

 1

了解到的知识点:

mybits

备注(其他):

 
//type为POJO的完整类名,alias为别名<br><typeAliases>
        <typeAlias type="com.xx.pojo.Student" alias="Student"/>
        <typeAlias type="com.xx.pojo.Class" alias="Class"/>
        <typeAlias type="com.xx.pojo.Teacher" alias="Teacher"/>
</typeAliases>//冗长问题<br>//name为POJO的包名<br>//默认起的别名为首字母小写<br><typeAliases>
      <package name="com.xx.pojo"/>
</typeAliases>
<settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>//日志
        <setting name="mapUnderscoreToCamelCase" value="true"/>//开启驼峰命名<br>     //开启一级缓存<br>     <setting name="cacheEnabled" value="true"/>//默认开启,增加可读性<br><cache/><br><em id="__mceDel"></settings></em>

typeHandlers(此标签必须放置environments前和typeAliases后)

environments

<environments default="development">

        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="url"/>
                <property name="username" value="name"/>
                <property name="password" value="pwd"/>
            </dataSource>
        </environment>
</environments>
<!--方式一-->
<mappers>
        <mapper resource="com/xx/dao/studentMapper.xml"/>
</mappers>
<!--方式二 不常见-->
<!--使用url引用本地文件>
<mappers>
        <mapper url="file://D:/com/xx/dao/student.xml"/>
</mappers>
<!--方式三-->
<!--使用class引入接口类-->
<!--接口名称与映射文件名称一致、接口与映射文件在同一个包中、映射文件mapper标签的命名空间的值为全限定类名-->
 <mappers>
        <mapper class="com.xx.dao.studentMapper"/>
</mappers>
<!--方式四-->
 <!--使用包名引入-->
  <!--DAO的实现类采用mapper动态代理实现、其他三个条件与类名引入条件一致-->
 <mappers>
       <package name="com.xx.dao"/>
 </mappers>
int addStu2(Map<String,Object> map);
<insert id="addStu2" parameterType="map">
        insert into dt_stu(id,studentName) values(#{sid},#{sname});
</insert>
@Test
public void addStu2()
{
        SqlSession sqlSession=mybatisUtil.getSqlSession();
        studentMapper mapper = sqlSession.getMapper(studentMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("sid",10);
        map.put("sname","xx");
        mapper.addStu2(map);
        sqlSession.commit();
        sqlSession.close();
}
<select id="getLikeStudent" resultType="Student" parameterType="String">
        select from dt_stu where studentName like "%"#{ssname}"%"
</select>
List<Student> stulist= mapper.getLikeStudent("%李%");
<select id="getLikeStudent" resultType="Student" parameterType="String">
        select from dt_stu where studentName concat ('%',#{value},'%')
</select>
<resultMap id="userMap" type="user">
        <result column="id" property="id"/>
        <result column="name" property="userName"/>
        <result column="age" property="age"/>
</resultMap>
<select id="getAllUser" resultMap="userMap">
        select from user
</select>

注解使用mybatis

@Select("select * from dt_stu")
List<Student> getAllStudents();

//方法存在多个参数,需添加@Param
@Select("select * from dt_stu where id = #{id} ")
Student getUserById(@Param("id") int id);

@Insert("insert into dt_stu(id,studentName,gender,age) values(#{id},#{studentName},#{gender},#{age})")
int addStudent(Student stu);
@Update("update dt_stu set studentName=#{studentName},gender=#{gender},age=#{age} where id=#{id}")
int updateStudent(Student student);
@Delete("delete from dt_stu where id = #{id}")
int deleteStu(@Param("id") int id);
posted @   平安喜乐×  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示