标签:
1.配置映射文件grilsMapper.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"> <mapper namespace="mapper.GrilsMapper"> <select id="getGril" parameterType="int" resultType="domain.Grils"> select * from grils where id=#{id} </select> <resultMap type="domain.Grils" id="grilsMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="nationality" property="nationality"/> </resultMap> <select id="getGrils" resultMap="grilsMap"> select * from grils </select> <insert id="addGril" parameterType="domain.Grils"> insert into grils values (#{id},#{name},#{nationality}) </insert> <update id="updateGril" parameterType="domain.Grils"> update grils set name=#{name},nationality=#{nationality} where id=#{id} </update> <delete id="deleteGril" parameterType="int"> delete from grils where id=#{id} </delete> </mapper>
映射文件配置很简单,需要什么就配置什么。对应的select update delete标签等。
2.在接口GrilsMapper中增加对应方法
package mapper; import java.util.List; import domain.Grils; public interface GrilsMapper { //这个方法名必须与映射文件的ID相同!!! public Grils getGril(int id); public void addGril(Grils gril); public List<Grils> getGrils(); public void updateGril(Grils gril); public void deleteGril(int id); }
3.创建测试类GrilsTest2
package test; import java.io.InputStream; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import domain.Grils; import mapper.GrilsMapper; public class GrilsTest2 { private static SqlSession sqlSession; private static GrilsMapper grilsMapper; static { String resource = "conf.xml"; InputStream in = GrilsTest.class.getClassLoader().getResourceAsStream(resource); //创建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in); sqlSession = sessionFactory.openSession(); grilsMapper = sqlSession.getMapper(GrilsMapper.class); } public static void main(String[] args) { GrilsTest2 grilsTest2 = new GrilsTest2(); grilsTest2.getGril(); grilsTest2.addGril(); grilsTest2.getGrils(); grilsTest2.updateGril(); grilsTest2.getGrils(); grilsTest2.deleteGril(); grilsTest2.getGrils(); } //查询一条 public void getGril() { Grils gril = grilsMapper.getGril(1); System.out.println("查询一条记录:"+gril); } //添加一条 public void addGril() { Grils gril = new Grils(); gril.setId(2); gril.setName("章子怡"); gril.setNationality("中国"); grilsMapper.addGril(gril); sqlSession.commit(); System.out.println("增加一条记录:"+gril); } //查询全部 public void getGrils() { List<Grils> grils = grilsMapper.getGrils(); System.out.println("查询全部:"+grils); } //更新某条记录 public void updateGril() { Grils gril = new Grils(); gril.setId(1); gril.setName("苍老师"); gril.setNationality("日本"); grilsMapper.updateGril(gril); sqlSession.commit(); System.out.println("更新了一条记录:"+gril); } //删除 public void deleteGril() { grilsMapper.deleteGril(2); System.out.println("删除了一条记录"); sqlSession.commit(); } }
4.打印结果
查询一条记录:Grils [id=1, name=苍井空, nationality=日本]
增加一条记录:Grils [id=2, name=章子怡, nationality=中国]
查询全部:[Grils [id=1, name=苍井空, nationality=日本], Grils [id=2, name=章子怡, nationality=中国]]
更新了一条记录:Grils [id=1, name=苍老师, nationality=日本]
查询全部:[Grils [id=1, name=苍老师, nationality=日本], Grils [id=2, name=章子怡, nationality=中国]]
删除了一条记录
查询全部:[Grils [id=1, name=苍老师, nationality=日本]]
5.看了大概也就半个小时的时间,就把基础掌握了。
标签:
原文地址:http://www.cnblogs.com/shichaohome/p/5104789.html