MyBatis的一些xml


MyBatis的核心配置文件

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
</configuration>

MyBatis中mapper接口的xml


<?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="org.mybatis.example.BlogMapper">
    <select id="selectBlog" resultType="Blog">
        select * from Blog where id = #{id}
    </select>
</mapper>

MyBatis的核心对象基本使用

InputStream inputStream = MybatisTest.class.getClassLoader().getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
userMapper = sqlSession.getMapper(UserMapper.class);

单独使用MyBatis分页助手

  • pom.xml

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>3.7.5</version>
</dependency>
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>1.2</version>
</dependency>

在MyBatis核心配置文件中声明分页插件

<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <property name="dialect" value="mysql"/>
    </plugin>
</plugins>

分页助手的基本使用参考

SpringBoot整合MyBatis分页助手


MyBatis动态sql的xml

<select id="findUserByCondition" resultType="user" parameterType="user">
    select * from user
    <where>
        <if test="id != 0 and id != null">
            and id=#{id}
        </if>
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="password != null">
            and password=#{password}
        </if>
    </where>
</select>

<select id="findUserByIds" resultType="user" parameterType="list">
    select * from user
    <where>
        <foreach collection="list" open="id in (" close=")" item="uid" separator=",">
            #{uid}
        </foreach>
    </where>
</select>


Mybatis实体映射

<resultMap id="userMap" type="user">
    <id property="id" column="id"/>
    <result property="username" column="username"/>
    <result property="password" column="password"/>
    <association property="department" javaType="department">
        <id property="id" column="did"/>
        <result property="dName" column="d_name"/>
    </association>
</resultMap>
<select id="selectAll" resultMap="userMap">
    SELECT * FROM dept d , USER u WHERE u.id=d.did
</select>


<resultMap id="deptMap" type="department">
    <id column="did" property="id"/>
    <result column="d_name" property="dName"/>
    <collection property="userList" ofType="user">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
    </collection>
</resultMap>
<select id="findAllDept" resultMap="deptMap">
    SELECT * FROM dept d , USER u WHERE u.id=d.did
</select>

<association></association> 标签映射单个实体对象 比如 private User user;

<collection></collection> 标签映射集合实体对象 比如 private List<User> users;

MyBatis的多表映射注解和单表映射注解 不推荐!!

  • 单表

@Delete("delete from user where id = #{id}")
void deleteUserById(Integer id);

@Insert("insert into user values(null,#{username},#{password})")
void insertUser(User user);

@Update("update user set username=#{username},password=#{password} where id=#{id} ")
void updateUser(User user);

@Select("select * from user where id = #{id}")
User  selectUserById(Integer id);

  • 多表

@Select("SELECT * FROM dept d , USER u WHERE u.id=d.did")
@Results({
    @Result(id = true,column ="id",property = "id"),
    @Result(column = "username",property = "username"),
    @Result(column = "password",property = "password"),
    @Result(column = "did",property = "department.id"),
    @Result(column = "d_name",property = "department.dName")
})
List<User> selectAll();


@Select("SELECT * FROM dept d , USER u WHERE u.id=d.did")
@Results({
    @Result(id = true,column = "did",property = "id"),
    @Result(column = "d_name",property = "dName"),
    @Result(
        column = "id",
        property = "userList",
        javaType = List.class,
        many = @Many(select = "cn.liuweiwei.mapper.UserMapper.selectUserById")
    )
})
List<Department> selectAllDept();

完结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值