码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis01

时间:2019-11-01 09:49:11      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:vat   ted   odi   closed   puts   env   obj   实现类   values   

---恢复内容开始---

导包

技术图片

 

 主要是mybatis包

在resources中写入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">
<configuration>
    <!--引入properties-->
    <properties resource="jdbc.properties"/>
    <typeAliases>
        <!-- 包的配置:项目,添加了包之后,类名就是别名 -->
        <package name="cn.jiedada.mybatis.doamin"/>
        <package name="cn.jiedada.mybatis.query"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <!--type   JDBC/ MANAGED   DBC – 这个配置就是直接使用了 JDBC 的提交和回滚设置, ||  MANAGED – 这个配置几乎没做什么。-->
            <transactionManager type="JDBC"/>
            <!--POOLED   mybaits 内置连接池-->
            <dataSource type="POOLED">
                <!--四大金刚-->
                <property name="driver" value="${jdbc.driverClassName}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--映射写sql的xml配置文件-->
        <mapper resource="cn/jiedada/mybatis/dao/impl/ProductMapper.xml"/>
    </mappers>
</configuration>
View Code

通过下面方法引入Mapper.XML

<mappers>
        <!--映射写sql的xml配置文件-->
        <mapper resource="cn/jiedada/mybatis/dao/impl/ProductMapper.xml"/>
    </mappers>

 

写入jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db1011
jdbc.username=root
jdbc.password=123456

 

在写入类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">
<!--namespace是我们自己取的名字-->
<mapper namespace="cn.jiedada.reposttory.ProductMapper">
    <!--通过namespace加上id找到我们的sql
    parameterType参数类型long->Long _long->long
    resultType返回值(每一天数据的(非常重要))类型
    /*id = #{id}用的是#
    */在里面不能写注解
    -->
    <select id="getOne" parameterType="long" resultType="cn.jiedada.mybatis.doamin.Product">
        select * from product where id = #{id}
    </select>
    <select id="getAll" resultType="Product">
        select * from product
    </select>
    <!-- useGeneratedKeys="true" 是否返回添加后主键的id
    keyProperty="id" 在类中主键名称
    keyColumn="id"  在数据库中的主键名称
    -->
    <insert id="save" parameterType="Product"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into product (productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
        values(
            #{productName},
            #{dir_id},
            #{salePrice},
            #{supplier},
            #{brand},
            #{cutoff},
            #{costPrice}
        )
    </insert>
    <update id="update" parameterType="Product">
        update product set
        productName=#{productName},
        dir_id=#{dir_id},
        salePrice=#{salePrice},
        supplier=#{supplier},
        brand=#{brand},
        cutoff=#{cutoff},
        costPrice=#{costPrice}
        where id=#{id}
    </update>
    <delete id="delete" parameterType="long">
        delete from product where id=#{id}
    </delete>
    <!--批量删除-->
    <delete id="deleteBatch" parameterType="java.util.List">
        delete from product where id
        in
        <foreach collection="list" index="index" item="id" open="("
                 separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <insert id="saveBatch" parameterType="arraylist"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        /*批量添加并且返回主键值*/
        insert into product (productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
        values
        <foreach collection="list" index="index" item="p" separator=",">
            (
            #{p.productName},
            #{p.dir_id},
            #{p.salePrice},
            #{p.supplier},
            #{p.brand},
            #{p.cutoff},
            #{p.costPrice}
            )
        </foreach>


    </insert>
    <!--高级查询-->
    <select id="selectLacation" parameterType="ProductQuery" resultType="product">
        select * from product
        <where>
            <if test="productName!=null">
                and  productName like #{productName}
            </if>
        </where>
    </select>
</mapper>
View Code

 

写一个工具类创建sqlSession

技术图片
package cn.jiedada.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public enum MyBatisUtil {
    /*通过枚举的方式这样就可以直接创建单列模式了
    * 必须添加INSTANCE这个属性,就相当于写了一个无参构造方法*/
    INSTANCE;
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            Reader reader = Resources.getResourceAsReader("mybaties-confing.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public SqlSession openSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
View Code

 

实现类

技术图片
package cn.jiedada.mybatis.dao.impl;

import cn.jiedada.mybatis.dao.IProductDao;

import cn.jiedada.mybatis.doamin.Product;
import cn.jiedada.mybatis.query.ProductQuery;
import cn.jiedada.mybatis.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import org.apache.ibatis.io.Resources;

import java.io.InputStream;
import java.io.Reader;
import java.util.List;

public class ProductDaoImpl implements IProductDao {
    @Override
    public void insert(Product product) throws IOException {


    }

    @Override
    public void update(Product product) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public Product findOne(Long id) {

        return null;
    }

    @Override
    public List<Product> findAll() throws IOException {
        SqlSession sqlSession = MyBatisUtil.INSTANCE.openSqlSession();
        List<Product> objects = sqlSession.selectList("cn.jiedada.reposttory.ProductMapper.getAll");
        return objects;
    }

    @Override
    public void deleteBatch(List aslist) {
        SqlSession sqlSession = MyBatisUtil.INSTANCE.openSqlSession();
        sqlSession.delete("cn.jiedada.reposttory.ProductMapper.deleteBatch",aslist);
        sqlSession.commit();
    }

    @Override
    public void saveBatch(List<Product> products) {
        SqlSession sqlSession = MyBatisUtil.INSTANCE.openSqlSession();
        sqlSession.insert("cn.jiedada.reposttory.ProductMapper.saveBatch",products);
        sqlSession.commit();
    }

    @Override
    public List<Product> selectLacation(ProductQuery query) {
        //cn.jiedada.reposttory.ProductMapper是我们Mapper.xml中的
        //<mapper namespace="cn.jiedada.reposttory.ProductMapper">这个名称
        //selectLacation是id值这样就可以通过这种方式找到该方法
        /*
        <select id="getAll" resultType="Product">
            select * from product
        </select>
        */
        SqlSession sqlSession = MyBatisUtil.INSTANCE.openSqlSession();
        List<Product> list = sqlSession.selectList("cn.jiedada.reposttory.ProductMapper.selectLacation", query);
        return list;
    }
}
View Code

 

实现CRUD

技术图片
 <!--通过namespace加上id找到我们的sql
    parameterType参数类型long->Long _long->long
    resultType返回值(每一天数据的(非常重要))类型
    /*id = #{id}用的是#
    */在里面不能写注解
    -->
    <select id="getOne" parameterType="long" resultType="cn.jiedada.mybatis.doamin.Product">
        select * from product where id = #{id}
    </select>
    <select id="getAll" resultType="Product">
        select * from product
    </select>
    <!-- useGeneratedKeys="true" 是否返回添加后主键的id
    keyProperty="id" 在类中主键名称
    keyColumn="id"  在数据库中的主键名称
    -->
    <insert id="save" parameterType="Product"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into product (productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
        values(
            #{productName},
            #{dir_id},
            #{salePrice},
            #{supplier},
            #{brand},
            #{cutoff},
            #{costPrice}
        )
    </insert>
    <update id="update" parameterType="Product">
        update product set
        productName=#{productName},
        dir_id=#{dir_id},
        salePrice=#{salePrice},
        supplier=#{supplier},
        brand=#{brand},
        cutoff=#{cutoff},
        costPrice=#{costPrice}
        where id=#{id}
    </update>
    <delete id="delete" parameterType="long">
        delete from product where id=#{id}
    </delete>
View Code

 

 

批量操作

技术图片
<!--批量删除-->
    <delete id="deleteBatch" parameterType="java.util.List">
        delete from product where id
        in
        <foreach collection="list" index="index" item="id" open="("
                 separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <insert id="saveBatch" parameterType="arraylist"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        /*批量添加并且返回主键值*/
        insert into product (productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
        values
        <foreach collection="list" index="index" item="p" separator=",">
            (
            #{p.productName},
            #{p.dir_id},
            #{p.salePrice},
            #{p.supplier},
            #{p.brand},
            #{p.cutoff},
            #{p.costPrice}
            )
        </foreach>


    </insert>
View Code

 

 

高级查

技术图片
 <!--高级查询-->
    <select id="selectLacation" parameterType="ProductQuery" resultType="product">
        select * from product
        <where>
            <if test="productName!=null">
                and  productName like #{productName}
            </if>
        </where>
    </select>
View Code

 

 

---恢复内容结束---

MyBatis01

标签:vat   ted   odi   closed   puts   env   obj   实现类   values   

原文地址:https://www.cnblogs.com/xiaoruirui/p/11774908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!