MyBatis入门
Posted ruijiege
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis入门相关的知识,希望对你有一定的参考价值。
导包
主要是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>
通过下面方法引入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>
写一个工具类创建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(); } }
实现类
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; } }
实现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>
批量操作
<!--批量删除--> <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>
询
以上是关于MyBatis入门的主要内容,如果未能解决你的问题,请参考以下文章
markdown [mybatis参考]关于mybatis #mybatis的一些片段