您现在的位置是:首页 > 个人日记个人日记
mybatis-02-CRUD简单操作
2019-02-02 13:58:15【个人日记】322人已围观
简介这是mybatis简单的CRUD操作,可以参考下里面的sql语句
CRUD配置
对商品数据的增删改查
项目结构

配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>test</groupId><artifactId>mybatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--log记录--><dependency><groupId>log4j</groupId><version>1.2.17</version></dependency><!--MySQL驱动--><dependency><groupId>mysql</groupId><version>5.1.6</version></dependency><!-- ibatis.jar --><dependency><version>3.4.6</version></dependency><!-- junit.jar --><dependency><groupId>junit</groupId><version>4.12</version><scope>test</scope></dependency></dependencies>
mybatis-config.xml与Product.java文件并未变化
Mapper文件映射
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="category"><!--查询所有--><select id="fingAll" resultType="com.huangxin.model.ProductCategory">SELECT * FROM product</select><!--查询一个--><select id="getById" parameterType="java.lang.Long" resultType="com.huangxin.model.ProductCategory">SELECT * FROM product WHERE id = #{id}</select><!--${只能放value}--><!--使用${}可能会有sql注入--><!--使用"%"#{*}"#"也可以--><select id="findByCategoryName" parameterType="java.lang.String" resultType="com.huangxin.model.ProductCategory">SELECT * FROM product WHERE supplier LIKE '%${value}%'</select><!--插入新数据--><insert id="insert" parameterType="com.huangxin.model.ProductCategory">INSERT INTO product(productName,salePrice,supplier,brand,costPrice)VALUES(#{productName},#{salePrice},#{supplier},#{brand},#{costPrice})</insert><!--修改数据--><update id="update" parameterType="com.huangxin.model.ProductCategory">UPDATE product SET supplier=#{supplier} WHERE id=#{id}</update><!--删除指定数据--><delete id="deleteById" parameterType="java.lang.Long">DELETE FROM product WHERE id = #{id}</delete></mapper>
加入log4j测试
将其放入resources目录
配置log4j.xml文件
#全局配置log4j.rootLogger=DEBUG,console#debug调试#控制台日志输出log4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m %n
Junit测试
配置DAO.java文件
package com.huangxin.dao;import com.huangxin.model.ProductCategory;import java.util.List;public interface ProductDAO {/*** 根据id删除信息** @param id*/void deleteById(Long id);/*** 通过ProductCategory对象插入信息** @param productCategory*/void insert(ProductCategory productCategory);/*** 更改商品信息** @param productCategory*/void update(ProductCategory productCategory);/*** 查询所有商品信息** @return*/List<ProductCategory> getAll();/*** 根据id查询信息** @param id*/ProductCategory getById(Long id);/*** 模糊查询** @param categoryName* @return*/List<ProductCategory> findByCategoryName(String categoryName);}
实现接口
配置DAOImlp.java文件
package com.huangxin.dao.impl;import com.huangxin.dao.ProductDAO;import com.huangxin.model.ProductCategory;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import java.util.List;public class ProductDAOImpl implements ProductDAO {private SqlSessionFactory factory;/*** 带参数的构造函数** @param factory*/public ProductDAOImpl(SqlSessionFactory factory) {this.factory = factory;}public void deleteById(Long id) {SqlSession sqlSession = factory.openSession(true);sqlSession.delete("category.deleteById", id);sqlSession.close();}public void insert(ProductCategory productCategory) {// 获取一个sqlSessionSqlSession sqlSession = factory.openSession(true);//设置事务是否自动提交sqlSession.insert("category.insert", productCategory);//sqlSession.commit();如果不自动提交事务,最后在这里提交sqlSession.close();}public void update(ProductCategory productCategory) {// 获取一个sqlSessionSqlSession sqlSession = factory.openSession(true);//设置事务是否自动提交sqlSession.update("category.update", productCategory);sqlSession.close();}public List<ProductCategory> getAll() {// 获取一个sqlSessionSqlSession sqlSession = factory.openSession();//赋值给listList<ProductCategory> list = sqlSession.selectList("category.fingAll");//关闭sqlSessionsqlSession.close();return list;}public ProductCategory getById(Long id) {//获取SqlSession会话SqlSession sqlSession = factory.openSession();ProductCategory category = sqlSession.selectOne("category.getById", id);sqlSession.close();return category;}public List<ProductCategory> findByCategoryName(String categoryName) {SqlSession sqlSession = factory.openSession();List<ProductCategory> list = sqlSession.selectList("category.findByCategoryName", categoryName);sqlSession.close();return list;}}
junit测试
package com.huangxin.dao.impl;import com.huangxin.dao.ProductDAO;import com.huangxin.model.ProductCategory;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import java.io.InputStream;import java.math.BigDecimal;import java.util.List;public class ProductDAOImplTest {private SqlSessionFactory factory;private ProductDAO dao;@Beforepublic void setUp() throws Exception {//将全局配置文件读取InputStream in = Resources.getResourceAsStream("mybatis-config.xml");factory = new SqlSessionFactoryBuilder().build(in);dao = new ProductDAOImpl(factory);}@Testpublic void deleteById() {dao.deleteById(22L);}@Testpublic void insert() {ProductCategory productCategory = new ProductCategory();productCategory.setProductName("测试");productCategory.setBrand("未知");productCategory.setCostPrice(new BigDecimal("50"));productCategory.setSupplier("未知");productCategory.setSalePrice(new BigDecimal("100"));dao.insert(productCategory);}@Testpublic void update() {ProductCategory productCategory = dao.getById(22L);productCategory.setSupplier("新数据");dao.update(productCategory);}@Testpublic void getAll() {List<ProductCategory> lists = dao.getAll();for (ProductCategory list : lists) {System.out.println(list);}}@Testpublic void getById() {ProductCategory productCategory = dao.getById(6l);System.out.println(productCategory);System.out.println(productCategory.getProductName());}@Testpublic void findByCategoryName1() {for (ProductCategory productCategory : dao.findByCategoryName("未知")) {System.out.println(productCategory);}}}
控制台输出结果

Tags: JavaWeb
下一篇: spring-12-事务管理-xml配置
相关文章
随机图文
评论区
2025-11-09 06:10:36
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
