文章目录 product.sql pom.xml mybatis-config.xml mapper.ProductsMapper.xml pojo.products.java utils.DButil.java mapper.impl.ProductsMapperImpl.java test
product.sql
create table products
(product_id int auto_increment comment '产品ID'primary key,product_name varchar(100) null comment '产品名称',brand varchar(50) null comment '品牌',price decimal(10, 2) null comment '价格',color varchar(20) null comment '颜色',storage_capacity varchar(10) null comment '存储容量',description text null comment '描述'
)comment '手机产品表';
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> com.aistat</ groupId> < artifactId> mybatis_tech</ artifactId> < version> 1.0-SNAPSHOT</ version> < packaging> jar</ packaging> < properties> < maven.compiler.source> 8</ maven.compiler.source> < maven.compiler.target> 8</ maven.compiler.target> < project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding> </ properties> < dependencies> < dependency> < groupId> com.mysql</ groupId> < artifactId> mysql-connector-j</ artifactId> < version> 8.0.31</ version> </ dependency> < dependency> < groupId> org.mybatis</ groupId> < artifactId> mybatis</ artifactId> < version> 3.5.15</ version> </ dependency> < dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.13.2</ version> < scope> test</ scope> </ dependency> </ dependencies> </ project>
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> < settings> < setting name = " mapUnderscoreToCamelCase" value = " true" /> </ settings> < environments default = " development" > < environment id = " development" > < transactionManager type = " JDBC" /> < dataSource type = " POOLED" > < property name = " driver" value = " com.mysql.cj.jdbc.Driver" /> < property name = " url" value = " jdbc:mysql://localhost:3306/dict" /> < property name = " username" value = " root" /> < property name = " password" value = " 123456" /> </ dataSource> </ environment> </ environments> < mappers> < mapper resource = " mapper/ProductsMapper.xml" > </ mapper> </ mappers>
</ configuration>
mapper.ProductsMapper.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 = " mapper.ProductsMapper" > < insert id = " insertOne" parameterType = " com.aistart.tech.pojo.Products" > insert into products (product_name,color,price)values (#{productName},#{color},#{price})</ insert> < select id = " selectOneById" parameterType = " int" resultType = " com.aistart.tech.pojo.Products" > select * from products where product_id = #{id}</ select>
</ mapper>
pojo.products.java
package com. aistart. tech. pojo ; public class Products { private Integer productId; private String productName; private String brand; private double price; private String color; private String storageCapacity; private String description; public Integer getProductId ( ) { return productId; } public void setProductId ( Integer productId) { this . productId = productId; } public String getProductName ( ) { return productName; } public void setProductName ( String productName) { this . productName = productName; } public String getBrand ( ) { return brand; } public void setBrand ( String brand) { this . brand = brand; } public double getPrice ( ) { return price; } public void setPrice ( double price) { this . price = price; } public String getColor ( ) { return color; } public void setColor ( String color) { this . color = color; } public String getStorageCapacity ( ) { return storageCapacity; } public void setStorageCapacity ( String storageCapacity) { this . storageCapacity = storageCapacity; } public String getDescription ( ) { return description; } public void setDescription ( String description) { this . description = description; } }
utils.DButil.java
package com. aistart. tech. 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. InputStream ; public class DButil { private static SqlSessionFactory sqlSessionFactory = null ; static {
try { InputStream inputStream = Resources . getResourceAsStream ( "mybatis-config.xml" ) ; sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ; } catch ( IOException e) { throw new RuntimeException ( e) ; } } public static SqlSession getSqlSession ( ) { return sqlSessionFactory. openSession ( ) ; } public static void close ( SqlSession session) { session. close ( ) ; } }
mapper.impl.ProductsMapperImpl.java
package com. aistart. tech. mapper. impl ; import com. aistart. tech. mapper. ProductsMapper ;
import com. aistart. tech. pojo. Products ;
import com. aistart. tech. utils. DButil ;
import org. apache. ibatis. session. SqlSession ; public class ProductsMapperImpl implements ProductsMapper { @Override public int insertOne ( Products products) { SqlSession sqlSession = DButil . getSqlSession ( ) ; System . out. println ( products. getProductName ( ) ) ; int rows = sqlSession. insert ( "insertOne" , products ) ; sqlSession. commit ( ) ; sqlSession. close ( ) ; return rows; } @Override public Products selectOneById ( int id) { SqlSession sqlSession = DButil . getSqlSession ( ) ; Products one = ( Products ) sqlSession. selectOne ( "selectOneById" , id) ; sqlSession. close ( ) ; return one; }
}
test
package com. aistart. tech. mapper. impl ; import com. aistart. tech. mapper. ProductsMapper ;
import com. aistart. tech. pojo. Products ;
import org. junit. Test ; public class ProductsMapperImplTest { ProductsMapper mapper = new ProductsMapperImpl ( ) ; @Test public void test ( ) { Products products = mapper. selectOneById ( 1 ) ; System . out. println ( products) ; products. setProductName ( "小米" ) ; products. setColor ( "红色" ) ; products. setPrice ( 200 ) ; System . out. println ( products. getProductName ( ) ) ; int rows = mapper. insertOne ( products) ; System . out. println ( rows) ; }
}