文章目录  product.sql pom.xml mybatis-config.xml mapper.ProductsMapper.xml pojo.products.java utils.DButil.java mapper.impl.ProductsMapperImpl.java test   
 
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 '手机产品表';
<?xml version="1.0" encoding="UTF-8"?> 
< projectxmlns = " 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> </ modelVersion> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < packaging> </ packaging> < properties> < maven.compiler.source> </ maven.compiler.source> < maven.compiler.target> </ maven.compiler.target> < project.build.sourceEncoding> </ project.build.sourceEncoding> </ properties> < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < scope> </ scope> </ dependency> </ dependencies> </ project> <?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> < settingname = " mapUnderscoreToCamelCase" value = " true" /> </ settings> < environmentsdefault = " development" > < environmentid = " development" > < transactionManagertype = " JDBC" /> < dataSourcetype = " POOLED" > < propertyname = " driver" value = " com.mysql.cj.jdbc.Driver" /> < propertyname = " url" value = " jdbc:mysql://localhost:3306/dict" /> < propertyname = " username" value = " root" /> < propertyname = " password" value = " 123456" /> </ dataSource> </ environment> </ environments> < mappers> < mapperresource = " mapper/ProductsMapper.xml" > </ mapper> </ mappers> </ configuration> <?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" > < mappernamespace = " mapper.ProductsMapper" > < insertid = " insertOne" parameterType = " com.aistart.tech.pojo.Products" > </ insert> < selectid = " selectOneById" parameterType = " int" resultType = " com.aistart.tech.pojo.Products" > </ select> </ mapper> 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; } } 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 ( ) ; } } 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; } 
} 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) ; } 
}