网上搜索下载就可以
1、将下载好的包导入项目中,build path
2、编写MyBatis配置文件:主要填写property标签中的四个value
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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!--数据库驱动、路径、用户名、密码 --> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hr_oa" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <!--resource是映射文件的路径 --> </mappers> </configuration>
3、建数据库对应的实体类
package cn.mg39.ssm01.entity; import java.io.Serializable; /** *实体类 SysGroup * */ public class SysGroup implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; //主键id private String name; //分组名 private String remark; //备注 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public SysGroup() { super(); // TODO Auto-generated constructor stub } public SysGroup(Integer id, String name, String remark) { super(); this.id = id; this.name = name; this.remark = remark; } public SysGroup(Integer id) { super(); this.id = id; } @Override public String toString() { return "SysGroup [id=" + id + ", name=" + name + ", remark=" + remark + "]"; } }
一些增删改查的方法
package cn.mg39.ssm01.dao; import java.util.List; import cn.mg39.ssm01.entity.SysGroup; /** * SysGroup的Dao * */ public interface SysGroupDao { /** * 增删改查接口 * @param sysGroup * @return */ public Integer insert(SysGroup sysGroup); public Integer delete(SysGroup sysGroup); public Integer update(SysGroup sysGroup); public List<SysGroup> selectAll(); public SysGroup selectById(SysGroup sysGroup); }
SysGroupDaoMapper.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"> <!--命名空间,在测试时需要调使用 ,可以为多个实体添加命名空间,只要nameSpace的值不同 --> <mapper namespace="cn.mg39.ssm01.dao.SysGroupDao"> <!--parameterType实体的全类名 (传给数据库的参数类型) --> <!--#后的变量名要和实体的变量名一致 --> <!--如果有外键,直接在变量名后.外键名。如 #{tbA.id} --> <!--增删改查 --> <insert id="insert" parameterType="cn.mg39.ssm01.entity.SysGroup"> insert into sys_group(name,remark) values(#{name},#{remark}) </insert> <delete id="delete" parameterType="cn.mg39.ssm01.entity.SysGroup"> delete from sys_group where id = #{id} </delete> <update id="update" parameterType="cn.mg39.ssm01.entity.SysGroup"> update sys_group set name = #{name} ,remark = #{remark} where id = #{id} </update> <select id="selectAll" resultType="cn.mg39.ssm01.entity.SysGroup"> select * from sys_group </select> <!--resultType(返回值类型,只有查询全部时需要,因为要接收传回的数据) --> <select id="selectById" parameterType="cn.mg39.ssm01.entity.SysGroup" resultType="cn.mg39.ssm01.entity.SysGroup"> select * from sys_group where id = #{id} </select> </mapper>
在myBatis-config.xml的<mappers>中添加Mapper映射,测试时会找mapper
<mappers> <!--resource是映射文件的路径,Mapper文件放在cn.mg39.ssm01.dao下。 --> <mapper resource="cn/mg39/ssm01/dao/SysGroupDaoMapper.xml" /> </mappers>
(删除,修改和插入差不多)
package cn.mg39.ssm01.test; import java.io.IOException; import java.io.Reader; 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 cn.mg39.ssm01.entity.SysGroup; /** * mybatis的增加测试 * */ public class InsertSysGroup { public static void main(String[] args) throws IOException { //1、读取配置文件 Reader reader = Resources.getResourceAsReader("myBatis-config.xml"); //2、创建sqlSessionFactoryBuilder(工人) SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3、创建SqlSessionFactory(建厂,将配置文件加入工厂) SqlSessionFactory factory = builder.build(reader); //4、打开SqlSession(通过工厂加工需要的数据) SqlSession session = factory.openSession(); //5、操作数据(实例化对象) SysGroup sysGroup = new SysGroup(); //id是自增长,不需要插入 //插入外键的操作 //sysGroup.set外键名(new 外键表(数据)); sysGroup.setName("测试数据的name"); sysGroup.setRemark("测试数据的remark"); //命名空间+id,通过session找配置文件,再通过配置文件找到映射文件, //再通过命名空间找到需要进行数据操作的实体,通过id名找到对象的CRUD操作。 //最后存session int rows = session.insert("cn.mg39.ssm01.dao.SysGroupDao.insert", sysGroup); System.out.println(rows > 0 ? "插入成功":"插入失败"); //6、提交事务(查询全部不需要提交事务) session.commit(); //7、关闭sqlSession session.close(); } }
package cn.mg39.ssm01.test; import java.io.IOException; import java.io.Reader; 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 cn.mg39.ssm01.entity.SysGroup; public class SelectAllSysGroup { public static void main(String[] args) throws IOException { // 加载配置文件 Reader reader = Resources.getResourceAsReader("myBatis-config.xml"); // 工人 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); // 工厂 SqlSessionFactory ssf = ssfb.build(reader); // 打开session SqlSession session = ssf.openSession(); // 操作数据 SysGroup sysGroup = new SysGroup(); List<SysGroup> list = session.selectList("cn.mg39.ssm01.dao.SysGroupDao.selectAll", sysGroup); //循环list取值 for (SysGroup sys : list) { //将获取到的数据赋值给对象,最后打印 sysGroup.setId(sys.getId()); sysGroup.setName(sys.getName()); sysGroup.setRemark(sys.getRemark()); System.out.println(sysGroup); } // 提交事务 session.commit(); // 关闭session session.close(); } }
原文:https://www.cnblogs.com/zhangzimuzjq/p/12057439.html