pom.xml
4.0.0
com.mybatis
mybatis-basic2
0.0.1-SNAPSHOT
org.mybatis
mybatis
3.2.8
mysql
mysql-connector-java
5.1.40
mybatis-config.xml
dbconfig.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=g
ProductMapper.xml
product
from
insert into
(
id,product_name,store,created,updated
)
values
(
null,#{productName},#{store},now(),now()
)
update
product_name=#{productName},
store=#{store},
updated=now(),
where id=#{id}
delete from
where id=#{id}
Product.java
package com.project.pojo;
import java.util.Date;
public class Product {
private Integer id;
private String productName;
private Integer store;
private Date created;
private Date updated;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getStore() {
return store;
}
public void setStore(Integer store) {
this.store = store;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
@Override
public String toString() {
return "Product [id=" + id + ", productName=" + productName + ", store=" + store + ", created=" + created
+ ", updated=" + updated + "]";
}
}
ProductDao.java
package com.project.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.project.pojo.Product;
public interface ProductDao {
/**
* 接口中方法名要与mapper文件中sqlid相同
* 返回值类型要与sqlid对象的返回值类型匹配
* @return
*/
ListfindAll();
Product findById(Integer id);
Integer insertObject(Product product);
List findProduct(@Param("columnName")String columnName);
List findByName(String proudctName);
Integer updateObject(Product product);
Integer deleteObject(Integer id);
}
TestProduct.java
package com.project.test;
import java.io.IOException;
import java.util.Date;
import java.util.List;
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 com.project.dao.ProductDao;
import com.project.pojo.Product;
public class TestProduct {
private static SqlSessionFactory factory;
public static void init() throws IOException{
factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
}
public static void findAll()throws IOException{
init();
SqlSession session=factory.openSession();
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
List list = dao.findAll();
System.out.println(list);
//释放资源
session.close();
}
public static void findById()throws IOException{
init();
SqlSession session=factory.openSession();
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
Product product = dao.findById(1);
System.out.println(product);
//释放资源
session.close();
}
public static void findByName()throws IOException{
init();
SqlSession session=factory.openSession();
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
List list = dao.findByName("apple");
System.out.println(list);
//释放资源
session.close();
}
public static void findProduct()throws IOException{
init();
SqlSession session=factory.openSession();
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
List list = dao.findProduct("store");
System.out.println(list);
//释放资源
session.close();
}
public static void insertObject()throws IOException{
init();
SqlSession session=factory.openSession(true);
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
Product product=new Product();
product.setProductName("pineapple");
product.setStore(70);
Integer row=dao.insertObject(product);
System.out.println(row);
System.out.println(product);
//释放资源
session.close();
}
public static void updateObject()throws IOException{
init();
SqlSession session=factory.openSession(true);
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
Product product=new Product();
product.setProductName("banana");
product.setStore(20);
product.setUpdated(new Date());
product.setId(1);
Integer row=dao.updateObject(product);
System.out.println(row);
//释放资源
session.close();
}
public static void deleteObject()throws IOException{
init();
SqlSession session=factory.openSession(true);
//通过session获取一个dao对象
ProductDao dao = session.getMapper(ProductDao.class);
Integer row=dao.deleteObject(2);
System.out.println(row);
//释放资源
session.close();
}
public static void main(String[] args) {
try {
insertObject();
System.out.println("============");
findAll();
System.out.println("============");
findByName();
System.out.println("============");
updateObject();
System.out.println("============");
deleteObject();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
1
Product [id=3, productName=pineapple, store=70, created=null, updated=null]
============
[Product [id=1, productName=apple, store=50, created=Tue Mar 19 00:00:00 CST 2019, updated=Tue Mar 19 00:00:00 CST 2019], Product [id=2, productName=pear, store=60, created=Tue Mar 19 00:00:00 CST 2019, updated=Tue Mar 19 00:00:00 CST 2019], Product [id=3, productName=pineapple, store=70, created=Tue Mar 19 00:00:00 CST 2019, updated=Tue Mar 19 00:00:00 CST 2019]]
============
[Product [id=1, productName=apple, store=50, created=Tue Mar 19 00:00:00 CST 2019, updated=Tue Mar 19 00:00:00 CST 2019], Product [id=3, productName=pineapple, store=70, created=Tue Mar 19 00:00:00 CST 2019, updated=Tue Mar 19 00:00:00 CST 2019]]
============
1
============
1