Mybatis基础:增删改查、模糊查询、多条件查询http://www.bieryun.com/3132.html
1、新建测试数据库,根据实体类属性创建
2、实体类
- package com.entity;
- /**
- * 数据库实体类
- * Created by lvjun on 2018-04-12.
- */
- public class Category {
- private int id;
- private String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- //显示中文
- @Override
- public String toString() {
- return "Category{" +
- "id=" + id +
- ", name='" + name + '\'' +
- '}';
- }
- }
3、配置文件Category.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="com.entity">
- <!--列表-->
- <select id="listCategory" resultType="Category">
- select * from category
- </select>
- <!--删除-->
- <delete id="deleteCategory" parameterType="Category">
- delete from category where id= #{id}
- </delete>
- <!--查询单个-->
- <select id="getCategory" parameterType="_int" resultType="Category">
- select * from category where id= #{id}
- </select>
- <!--修改-->
- <update id="updateCategory" parameterType="Category">
- update category set name=#{name} where id=#{id}
- </update>
- <!--添加-->
- <insert id="addCategory" parameterType="Category">
- insert into category ( name ) values (#{name})
- </insert>
- <!--模糊查询-->
- <select id="listCategoryByName" parameterType="string" resultType="Category">
- select * from category where name like concat('%',#{0},'%')
- </select>
- <!--多条件查询-->
- <select id="listCategoryByIdAndName" resultType="Category">
- select * from category
- <where>
- <if test="id!=null">
- and id > #{id}
- </if>
- <if test="name!=null">
- and name like concat('%',#{name},'%')
- </if>
- </where>
- </select>
- </mapper>
4、配置文件mybatis-config.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>
- <typeAliases>
- <package name="com.entity"/>
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/school?characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="1234"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="Category.xml"/>
- </mappers>
- </configuration>
5、测试类
- package com.lvjun;
- import com.entity.Category;
- 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.InputStream;
- import java.util.List;
- /**
- * 查询列表
- * Created by lvjun on 2018-04-12.
- */
- public class TestSelectList {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- List<Category> cs = session.selectList("listCategory");
- for (Category c : cs) {
- System.out.println(c.getId()+" "+c.getName());
- }
- }
- }
- package com.lvjun;
- import com.entity.Category;
- 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.InputStream;
- import java.util.List;
- /**
- * 查询一条记录
- * Created by lvjun on 2018-04-12.
- */
- public class TestSelectOne {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- Category c = session.selectOne("getCategory", 3);
- System.out.println(c.getId()+" "+c.getName());
- session.commit();
- session.close();
- }
- }
- package com.lvjun;
- import com.entity.Category;
- 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.InputStream;
- import java.util.List;
- /**
- * 模糊查询
- * Created by lvjun on 2018-04-12.
- */
- public class TestSelectLike {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- List<Category> cs = session.selectList("listCategoryByName", "win");
- for (Category c : cs) {
- System.out.println(c.getId()+" "+c.getName());
- }
- session.commit();
- session.close();
- }
- }
- package com.lvjun;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.entity.Category;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- /**
- * 多条件查询
- * Created by lvjun on 2018-04-12.
- */
- public class TestSelectToLike {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- Map<String, Object> params = new HashMap<>();
- params.put("id", 3); //条件查询
- params.put("name", "wi");
- List<Category> cs = session.selectList("listCategoryByIdAndName", params);
- for (Category c : cs) {
- System.out.println(c.getId()+" "+c.getName());
- }
- session.commit();
- session.close();
- }
- }
- package com.lvjun;
- import com.entity.Category;
- 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.InputStream;
- import java.util.List;
- /**
- * 修改记录
- * Created by lvjun on 2018-04-12.
- */
- public class TestUpdate {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- Category c = session.selectOne("getCategory", 2);
- c.setName("Android 8");
- session.update("updateCategory", c);
- listAll(session);
- session.commit();
- session.close();
- }
- private static void listAll(SqlSession session) {
- List<Category> cs = session.selectList("listCategory");
- for (Category c : cs) {
- System.out.println(c.getId()+" "+c.getName());
- }
- }
- }
- package com.lvjun;
- import com.entity.Category;
- 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.InputStream;
- import java.util.List;
- /**
- * 插入一条记录
- * Created by lvjun on 2018-04-12.
- */
- public class TestInsert {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- Category c = new Category();
- c.setName("新增加的数据");
- session.insert("addCategory", c);
- listAll(session);
- session.commit();
- session.close();
- }
- private static void listAll(SqlSession session) {
- List<Category> cs = session.selectList("listCategory");
- for (Category c : cs) {
- System.out.println(c.getId()+" "+c.getName());
- }
- }
- }
- package com.lvjun;
- import com.entity.Category;
- 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.InputStream;
- import java.util.List;
- /**
- * 删除一条记录
- * Created by lvjun on 2018-04-12.
- */
- public class TestDeleteOne {
- public static void main(String[] args) throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- Category c = new Category();
- c.setId(6);
- session.delete("deleteCategory", c);
- listAll(session);
- session.commit();
- session.close();
- }
- private static void listAll(SqlSession session) {
- List<Category> cs = session.selectList("listCategory");
- for (Category c : cs) {
- System.out.println(c.getId()+" "+c.getName());
- }
- }
- }