您现在的位置是:首页 > 个人日记个人日记

mybatis-02-CRUD简单操作

2019-02-02 13:58:15【个人日记】228人已围观

简介这是mybatis简单的CRUD操作,可以参考下里面的sql语句

CRUD配置

对商品数据的增删改查

项目结构

配置pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>test</groupId>
  7. <artifactId>mybatis</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--log记录-->
  11. <dependency>
  12. <groupId>log4j</groupId>
  13. <version>1.2.17</version>
  14. </dependency>
  15. <!--MySQL驱动-->
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <version>5.1.6</version>
  19. </dependency>
  20. <!-- ibatis.jar -->
  21. <dependency>
  22. <version>3.4.6</version>
  23. </dependency>
  24. <!-- junit.jar -->
  25. <dependency>
  26. <groupId>junit</groupId>
  27. <version>4.12</version>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>

mybatis-config.xmlProduct.java文件并未变化

Mapper文件映射

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="category">
  6. <!--查询所有-->
  7. <select id="fingAll" resultType="com.huangxin.model.ProductCategory">
  8. SELECT * FROM product
  9. </select>
  10. <!--查询一个-->
  11. <select id="getById" parameterType="java.lang.Long" resultType="com.huangxin.model.ProductCategory">
  12. SELECT * FROM product WHERE id = #{id}
  13. </select>
  14. <!--${只能放value}-->
  15. <!--使用${}可能会有sql注入-->
  16. <!--使用"%"#{*}"#"也可以-->
  17. <select id="findByCategoryName" parameterType="java.lang.String" resultType="com.huangxin.model.ProductCategory">
  18. SELECT * FROM product WHERE supplier LIKE '%${value}%'
  19. </select>
  20. <!--插入新数据-->
  21. <insert id="insert" parameterType="com.huangxin.model.ProductCategory">
  22. INSERT INTO product(productName,salePrice,supplier,brand,costPrice)
  23. VALUES(#{productName},#{salePrice},#{supplier},#{brand},#{costPrice})
  24. </insert>
  25. <!--修改数据-->
  26. <update id="update" parameterType="com.huangxin.model.ProductCategory">
  27. UPDATE product SET supplier=#{supplier} WHERE id=#{id}
  28. </update>
  29. <!--删除指定数据-->
  30. <delete id="deleteById" parameterType="java.lang.Long">
  31. DELETE FROM product WHERE id = #{id}
  32. </delete>
  33. </mapper>

加入log4j测试

将其放入resources目录
配置log4j.xml文件

  1. #全局配置
  2. log4j.rootLogger=DEBUG,console
  3. #debug调试
  4. #控制台日志输出
  5. log4j.appender.console=org.apache.log4j.ConsoleAppender
  6. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m %n

Junit测试

配置DAO.java文件

  1. package com.huangxin.dao;
  2. import com.huangxin.model.ProductCategory;
  3. import java.util.List;
  4. public interface ProductDAO {
  5. /**
  6. * 根据id删除信息
  7. *
  8. * @param id
  9. */
  10. void deleteById(Long id);
  11. /**
  12. * 通过ProductCategory对象插入信息
  13. *
  14. * @param productCategory
  15. */
  16. void insert(ProductCategory productCategory);
  17. /**
  18. * 更改商品信息
  19. *
  20. * @param productCategory
  21. */
  22. void update(ProductCategory productCategory);
  23. /**
  24. * 查询所有商品信息
  25. *
  26. * @return
  27. */
  28. List<ProductCategory> getAll();
  29. /**
  30. * 根据id查询信息
  31. *
  32. * @param id
  33. */
  34. ProductCategory getById(Long id);
  35. /**
  36. * 模糊查询
  37. *
  38. * @param categoryName
  39. * @return
  40. */
  41. List<ProductCategory> findByCategoryName(String categoryName);
  42. }

实现接口

配置DAOImlp.java文件

  1. package com.huangxin.dao.impl;
  2. import com.huangxin.dao.ProductDAO;
  3. import com.huangxin.model.ProductCategory;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import java.util.List;
  7. public class ProductDAOImpl implements ProductDAO {
  8. private SqlSessionFactory factory;
  9. /**
  10. * 带参数的构造函数
  11. *
  12. * @param factory
  13. */
  14. public ProductDAOImpl(SqlSessionFactory factory) {
  15. this.factory = factory;
  16. }
  17. public void deleteById(Long id) {
  18. SqlSession sqlSession = factory.openSession(true);
  19. sqlSession.delete("category.deleteById", id);
  20. sqlSession.close();
  21. }
  22. public void insert(ProductCategory productCategory) {
  23. // 获取一个sqlSession
  24. SqlSession sqlSession = factory.openSession(true);//设置事务是否自动提交
  25. sqlSession.insert("category.insert", productCategory);
  26. //sqlSession.commit();如果不自动提交事务,最后在这里提交
  27. sqlSession.close();
  28. }
  29. public void update(ProductCategory productCategory) {
  30. // 获取一个sqlSession
  31. SqlSession sqlSession = factory.openSession(true);//设置事务是否自动提交
  32. sqlSession.update("category.update", productCategory);
  33. sqlSession.close();
  34. }
  35. public List<ProductCategory> getAll() {
  36. // 获取一个sqlSession
  37. SqlSession sqlSession = factory.openSession();
  38. //赋值给list
  39. List<ProductCategory> list = sqlSession.selectList("category.fingAll");
  40. //关闭sqlSession
  41. sqlSession.close();
  42. return list;
  43. }
  44. public ProductCategory getById(Long id) {
  45. //获取SqlSession会话
  46. SqlSession sqlSession = factory.openSession();
  47. ProductCategory category = sqlSession.selectOne("category.getById", id);
  48. sqlSession.close();
  49. return category;
  50. }
  51. public List<ProductCategory> findByCategoryName(String categoryName) {
  52. SqlSession sqlSession = factory.openSession();
  53. List<ProductCategory> list = sqlSession.selectList("category.findByCategoryName", categoryName);
  54. sqlSession.close();
  55. return list;
  56. }
  57. }

junit测试

  1. package com.huangxin.dao.impl;
  2. import com.huangxin.dao.ProductDAO;
  3. import com.huangxin.model.ProductCategory;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import java.io.InputStream;
  10. import java.math.BigDecimal;
  11. import java.util.List;
  12. public class ProductDAOImplTest {
  13. private SqlSessionFactory factory;
  14. private ProductDAO dao;
  15. @Before
  16. public void setUp() throws Exception {
  17. //将全局配置文件读取
  18. InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
  19. factory = new SqlSessionFactoryBuilder().build(in);
  20. dao = new ProductDAOImpl(factory);
  21. }
  22. @Test
  23. public void deleteById() {
  24. dao.deleteById(22L);
  25. }
  26. @Test
  27. public void insert() {
  28. ProductCategory productCategory = new ProductCategory();
  29. productCategory.setProductName("测试");
  30. productCategory.setBrand("未知");
  31. productCategory.setCostPrice(new BigDecimal("50"));
  32. productCategory.setSupplier("未知");
  33. productCategory.setSalePrice(new BigDecimal("100"));
  34. dao.insert(productCategory);
  35. }
  36. @Test
  37. public void update() {
  38. ProductCategory productCategory = dao.getById(22L);
  39. productCategory.setSupplier("新数据");
  40. dao.update(productCategory);
  41. }
  42. @Test
  43. public void getAll() {
  44. List<ProductCategory> lists = dao.getAll();
  45. for (ProductCategory list : lists) {
  46. System.out.println(list);
  47. }
  48. }
  49. @Test
  50. public void getById() {
  51. ProductCategory productCategory = dao.getById(6l);
  52. System.out.println(productCategory);
  53. System.out.println(productCategory.getProductName());
  54. }
  55. @Test
  56. public void findByCategoryName1() {
  57. for (ProductCategory productCategory : dao.findByCategoryName("未知")) {
  58. System.out.println(productCategory);
  59. }
  60. }
  61. }

控制台输出结果

Tags: JavaWeb  

评论区

    2024-04-19 08:01:16

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我