MyBatis框架总结

pom.xml配置文件

资源导入

mysql

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

mybatis

 <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

junit

 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

log4j

日志打印

<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>

pagehelper

分页

  <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.7.5</version>
</dependency>
PageHelper.startPage(2,3);
PageInfo<User> pageInfo = new PageInfo<User>(userList);
System.out.println(pageInfo);

jsqlparser

JsqlParser插件用来对于SQL语句进行解析和组装,将SQL语句关键词之间的内容用List进行保存,同时可以进行更改List的内容后重新组装成一个新的SQL语句

<dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>

sqlMapConfig.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">

properties(属性)

 <properties resource="jdbc.properties"></properties>

导入jdbc配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=5212

typeAliases(类型别名)

<typeAliases>
        <typeAlias type="domain.User" alias="user"></typeAlias>
        <typeAlias type="domain.Order" alias="order"></typeAlias>
    </typeAliases>

environments(环境集合属性对象)

transactionManager(事务管理)

dataSource(数据源)

<environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

mappers(映射器)

直接映射到Dao接口

<mappers>
        <package name="dao"/>
    </mappers>

映射文件

<mappers>
        <mapper resource="mapper\UserMapper.xml"></mapper>
        <mapper resource="mapper\OrderMapper.xml"></mapper>
    </mappers>

UserMapper.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">

绑定Dao接口

<mapper namespace="dao.UserMapper"></mapper>

映射文件中的namespace是用于绑定Dao接口的

查询语句

id为接口方法名

<sql id="selectUser">select * from user</sql>

查询语句多对多

UserMapper

<resultMap id="userMap" type="domain.User">
        <id column="uid" property="id"></id>
        <result column="name" property="name"></result>
        <result column="password" property="password"></result>
        <result column="address" property="address"></result>
        <result column="phone" property="phone"></result>
        <result column="birthday" property="birthday"></result>

        <collection property="orderList" ofType="domain.Order">
           <id column="oid" property="id"></id>
           <result column="ordertime" property="ordertime"></result>
           <result column="ordername" property="ordername"></result>
        </collection>
    </resultMap>
    <select id="findAllOrder" resultMap="userMap">
        SELECT * ,o.id oid,u.id uid FROM USER u , orders o WHERE o.uid=u.id
    </select>

dao.Mapper

@Select(" select * from user ")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "password",property = "password"),
            @Result (column = "address",property = "address"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "birthday",property = "birthday"),
            @Result(
                    property = "orderList",
                    column = "id",
                    javaType = List.class,
                    many=@Many(select = "dao.OrderMapper.findByUid")
            ),


    }
    )
    public List<User> findUserAndOrder() throws IOException;

查询语句一对多

UserMapper

<resultMap id="orderMap" type="order">
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="ordername" property="ordername"></result>
        <!--<result column="name" property="user.name"></result>
        <result column="password" property="user.password"></result>
        <result column="address" property="user.address"></result>
        <result column="phone" property="user.phone"></result>
        <result column="birthday" property="user.birthday"></result>
-->
        <association property="user" javaType="user">
            <id column="uid" property="id"></id>
            <result column="name" property="name"></result>
            <result column="password" property="password"></result>
            <result column="address" property="address"></result>
            <result column="phone" property="phone"></result>
            <result column="birthday" property="birthday"></result>


        </association>
    </resultMap>
    <select id="findAll" resultMap="orderMap">
        SELECT * ,o.id oid,u.id uid FROM orders o , USER u WHERE o.uid=u.id
    </select>

dao.Mapper

@Select(" SELECT * FROM orders ")
    @Results({
            @Result(column = "oid",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "ordername",property = "ordername"),
            @Result(
                    property = "user",
                    column = "uid",
                    javaType = User.class,
                    one=@One(select = "dao.UserMapper.findById")
            ),
            })
    public List<Order> findAllo() throws IOException;


DateTypeHandler

package handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class DateTypeHandler extends BaseTypeHandler<Date> {

    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time = date.getTime();
        preparedStatement.setLong(i,time);
    }

    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        long aLong = resultSet.getLong(s);
        Date date = new Date(aLong);

        return date;
    }

    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        long aLong = resultSet.getLong(i);
        Date date = new Date(aLong);

        return date;
    }

    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long aLong = callableStatement.getLong(i);
        Date date = new Date(aLong);

        return date;

    }
}

动态sql

<select id="findByCondition" parameterType="user" resultType="user">
        select * from user
        <where>
        <if test="id!=0">
        and id=#{id}
        </if>
        <if test="name!=null">
            and name=#{name}
        </if>
        <if test="password!=null">
            and password=#{password}
        </if>
        <if test="address!=null">
            and address=#{address}
        </if>
        <if test="phone!=null">
            and phone=#{phone}
        </if>
        </where>
    </select>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值