MyBatis的基本配置可参考我的博客:MyBatis的基本配置以及简单的功能实现
链接:link
项目结构
通过MyBatis操作数据库,从读取核心配置文件,到拿到SqlSession对象区间为公共代码,可以抽取成为一个工具类MyBatisUtil
//SqlSessionFactoryBuilder -> SqlSessionFactory -> SqlSession
@Override
public Product findOne(Long id) {
try {
//1.读取核心配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//2.创建一个对象:SqlSessionFactory
// SqlSessionFactoryBuilder:构造者模式
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
//3.创建一个对象:SqlSession
SqlSession session = factory.openSession();
//4.session操作数据库
Product product = session.selectOne("cn.dieu.domain.ProductMapper.findOne",id);
return product;
} catch (Exception e)
e.printStackTrace();
}finally{
session.close();
}
return null;
}
MyBatisUtil.java
public class MyBatisUtil {
private static SqlSessionFactory factory;
static {
try {
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession openSession(){
return factory.openSession();
}
}
注:MyBatis操作数据库的SQL语句需要写在xml文件里
闲话少说直接上代码!!!
ProductMapper.xml
INSERT INTO product(productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
VALUES (#{productName},#{dir_id},#{salePrice},#{supplier},#{brand},#{cutoff},#{costPrice})
UPDATE product SET productName=#{productName},salePrice=#{salePrice},costPrice=#{costPrice},
cutoff=#{cutoff},supplier=#{supplier},brand=#{brand},dir_id=#{dir_id}
WHERE id=#{id}
DELETE FROM product WHERE id=#{id}
Product.java
public class Product {
private Long id;
private String productName;
private Long dirId;
private Double salePrice;
private String supplier;
private String brand;
private Double cutoff;
private Double costPrice;
//省略getter/setter和toString方法
}
IProductDao
public interface IProductDao {
void save(Product product);
void update(Product product);
void delete(Long id);
Product findOne(Long id);
List findAll();
}
impl>ProductDaoImpl.java
public class ProductDaoImpl implements IProductDao {
/**
* 添加数据
* @param product
*/
@Override
public void save(Product product) {
SqlSession session = null;
try {
session = MyBatisUtil.openSession();
session.insert("cn.dieu.domain.ProductMapper.save", product);
session.commit();
} catch (Exception e) {
session.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/**
* 修改数据
* @param product
*/
@Override
public void update(Product product) {
SqlSession session = null;
try {
session = MyBatisUtil.openSession();
session.update("cn.dieu.domain.ProductMapper.update", product);
session.commit();
} catch (Exception e) {
session.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/**
* 删除数据
* @param id
*/
@Override
public void delete(Long id) {
SqlSession session = null;
try {
session = MyBatisUtil.openSession();
session.delete("cn.dieu.domain.ProductMapper.delete",id );
session.commit();
} catch (Exception e) {
session.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/**
* 查询一条数据
* @param id
* @return
*/
@Override
public Product findOne(Long id) {
SqlSession session = null;
try {
session = MyBatisUtil.openSession();
return session.selectOne("cn.dieu.domain.ProductMapper.findOne", id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return null;
}
/**
* 查询所有数据
* @return
*/
@Override
public List findAll() {
SqlSession session = null;
try {
session = MyBatisUtil.openSession();
return session.selectList("cn.dieu.domain.ProductMapper.findAll");
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return null;
}
}
useGeneratedKeys:是否要返回id
keyColumn:数据库中的主键对应的列
keyProperty:domain中对应的主键属性
id返回到传过来的对象中
...
自定义别名 mybatis-config.xml
别名不区别大小写
注意配置顺序
resources>log4j.properties
#log4j.properties(日志文件:)
# ERROR错误的日志 WARN:警告 INFO:普通信息 DEBUG:调试日志 TRACE:日志
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
#把左边包名改成你自己的包名
log4j.logger.cn.dieu=TRACE
# 日志打印到控制台中
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 日志打印的一种格式(可以灵活地指定布局模式)
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 日志打印的格式是什么样子的 %d:日期 %p:优先级 %c:类的全名 %m:输出的结果 %n:换行
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n