其主要用到MyBatis jar包,也可以搜索其官网进行下载,这里我给一个我自己用的MyBatis版本MyBatis-3.5.7提取码为"fs33"以及相应的配置文件,如有不懂请看配置文件注释MyBatis.xml文件,其提取码为"nn8b"
以下代码是和数据库对接的具体案例
(1).包结构
(2).其项目的具体案例代码
package cn.flower.entity;
/**
* @version 1.0
* @author: 小原
* @date: 2021-06-17 13:32
*/
public class Flower {
private String id;
private String name;
private String anpthenmname;
private String property;
private String price;
private String production;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAnpthenmname() {
return anpthenmname;
}
public void setAnpthenmname(String anpthenmname) {
this.anpthenmname = anpthenmname;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
@Override
public String toString() {
return "Flower{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", anpthenmname='" + anpthenmname + '\'' +
", property='" + property + '\'' +
", price='" + price + '\'' +
", production='" + production + '\'' +
'}';
}
public Flower() {
}
/**
* flower有参构造器
* @param id 编号
* @param name 花名
* @param anpthenmname 花的别名
* @param property 花的科属
* @param price 花的价格
* @param production 花的生产地
*/
public Flower(String id, String name, String anpthenmname, String property, String price, String production) {
this.id = id;
this.name = name;
this.anpthenmname = anpthenmname;
this.property = property;
this.price = price;
this.production = production;
}
}
以下是接口中需要实现的方法
package cn.flower.dao;
import cn.flower.entity.Flower;
import java.util.List;
/**
* @version 1.0
* @author: 小原
* @date: 2021-06-17 14:30
*/
public interface FlowerDao {
public List<Flower> selectAll();
public List<Flower> selectAllById(String id);
public int insertFlower(Flower f);
public int deleteById(String id);
public int updateById(Flower f);
}
以下是实现类中具体的实现,切记在我们进行增删改的时间一定要调用一下commit()方法,不然不会报错也不会出现在你的数据库中产生受影响的行数
package cn.flower.dao.impl;
import cn.flower.dao.BaseMapper;
import cn.flower.dao.FlowerDao;
import cn.flower.entity.Flower;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
/**
* @version 1.0
* @author: 小原
* @date: 2021-06-17 14:30
*/
public class FlowerDaoImpl implements FlowerDao {
private SqlSession conn = BaseMapper.getConn();
FlowerDao mapper = conn.getMapper(FlowerDao.class);
@Override
public List<Flower> selectAll() {
List<Flower> flowers = mapper.selectAll();
return flowers;
}
@Override
public List<Flower> selectAllById(String id) {
List<Flower> flowers = mapper.selectAllById(id);
return flowers;
}
@Override
public int insertFlower(Flower f) {
int num = mapper.insertFlower(f);
conn.commit();
return num;
}
@Override
public int deleteById(String id) {
int num = mapper.deleteById(id);
conn.commit();
return num;
}
@Override
public int updateById(Flower f) {
int num = mapper.updateById(f);
conn.commit();
return num;
}
}
看了这样的多是不是有些迷茫了?有配置文件,但是我们貌似没有读取,其下就是我们的一个简单BaseMapper类,主要帮助我们建立连接等等
package cn.flower.dao;
import cn.flower.dao.impl.FlowerDaoImpl;
import cn.flower.entity.Flower;
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 org.jetbrains.annotations.Contract;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
* @version 1.0
* @author: 小原
* @date: 2021-06-17 13:37
*/
public class BaseMapper {
private static final String resource = "mybatis_config.xml";
private static SqlSessionFactory sqlSessionFactory;
static {
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
private static SqlSession conn;
/**
* 开启数据库的链接
* @return
*/
public static SqlSession getConn() {
return conn = sqlSessionFactory.openSession();
}
public static boolean close() {
if (conn != null) {
conn.close();
return true;
} else {
return false;
}
}
public static int executeUpdate(String sql) {
return 0;
}
// @Contract(pure = true)
// public static <T> List<T> executeQuery(Class<T> clz, String methodName) {
// conn.getMapper(clz);
// try {
// T t = clz.getConstructor().newInstance();
// Method method = clz.getMethod(methodName);
// conn.getMapper(clz);
// //System.out.println(((List<T>) invoke).size());
// } catch (InstantiationException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (InvocationTargetException e) {
// e.printStackTrace();
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// }
// //FlowerDao mapper = conn.getMapper(FlowerDao.class);
//
// return null;
// //return (List<T>) mapper.selectAll();
// //return (List<T>) flowers;
// }
}
紧接着就是我们的测试类中就需要调用这些方法啦
package cn.flower.test;
import cn.flower.dao.BaseMapper;
import cn.flower.dao.FlowerDao;
import cn.flower.dao.impl.FlowerDaoImpl;
import cn.flower.entity.Flower;
/**
* @version 1.0
* @author: 小原
* @date: 2021-06-17 13:31
*/
public class Test {
/**
* 程序的入口
* @param args 入口参数
*/
public static void main(String[] args) {
FlowerDao fd=new FlowerDaoImpl();
// name='矮牵牛', anpthenmname='碧冬茄',
// property='茄科碧冬茹属',
// price='2.5', production='南美阿根廷'
//fd.insertFlower(new Flower(null,"小原同志","小原","人类","10000","中国第一帅"));
Flower f=new Flower();
f.setId("9");
f.setName("王泽轩");
fd.updateById(f);
// fd.deleteById("6");
// fd.deleteById("7");
// fd.deleteById("8");
fd.selectAll().forEach(temp-> System.out.println(temp.toString()));
//fd.selectAllById("1").forEach(temp-> System.out.println(temp.toString()));
}
}
其下是效果:这里我们就只演示一个查询的效果了,剩下的小伙伴们运行代码即可看到了呢,嗯哼
这里呢我们顺带演示出了修改后的效果了,emmm
补充一下哈,另外我们要进行增删改查的xml文件忘了发了,嘻嘻!
<?xml version="1.0" encoding="UTF-8" ?>
<!--注意事项
(1).增删改查的标签中,id必须和类中的方法名相同
(2).resultType为实体类的全路径,有一点儿是例外的就是当是查询多少条记录的时间这个值为int 等等
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.flower.dao.FlowerDao">
<select id="selectAll" resultType="cn.flower.entity.Flower">
select * from flower
</select>
<select id="selectAllById" resultType="cn.flower.entity.Flower">
select * from flower where id=#{id}
</select>
<insert id="insertFlower" parameterType="cn.flower.entity.Flower">
INSERT INTO flower VALUES(NULL,#{name},#{anpthenmname},#{property},#{price},#{production})
</insert>
<delete id="deleteById" parameterType="cn.flower.entity.Flower">
delete from flower where id=#{id}
</delete>
<update id="updateById" parameterType="cn.flower.entity.Flower">
update flower set name =#{name} where id =#{id}
</update>
</mapper>