admin 管理员组文章数量: 887007
mybatis: 简单增删改
Mapper映射文件:
注: 映射文件中的sql语句不用带分号
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis//DTD Mapper 3.0//EN"".dtd">
<mapper namespace="com.mybatis.mapper.VendorMapper"><!-- 查询resultType : 指定查询出来的类型databaseId : 配合databaseIdProvider动态选择数据库SQL语句--><select id="getVendorById" resultType="com.mybatis.bean.Vendor" databaseId="mysql">select * from Vendors where vend_id = #{id}</select><!-- 增加: parameterType: 传入的参数类型(可以省略,mybatis会自动映射)--><insert id="insertVendor">INSERT INTO Vendors VALUES(#{vendId},#{vendName},#{vendAddress},#{vendCity},#{vendState},#{vendZip},#{vendCountry})</insert><!-- 修改: parameterType: 传入的参数类型(可以省略,mybatis会自动映射)--><update id="updateById">UPDATE Vendors SET vend_name = #{vendName} WHERE vend_id = #{vendId}</update><!-- 删除:parameterType: 传入的参数类型(可以省略,mybatis会自动映射)--><delete id="deleteById">DELETE FROM Vendors WHERE vend_id = #{vendId}</delete></mapper>
接口文件:
/** Mybatis中Insert、update、delete操作可以有返回值,返回值类型可以有以下几种:* Boolean(boolean) | Integer(int) | Long(long) | void* 前三种类型(包装类型或普通数据类型)* 如果是Integer 或 Long 类型,则返回的是影响行数,如果是boolean类型,mybatis会将数值类型转换成boolean类型返回(* 影响行数>0为true,影响行数=0为false)* *///查询public Vendor getVendorById(String vendId);//增加public Boolean insertVendor(Vendor vendor);//更新public Boolean updateById(Vendor vendor);//删除public Boolean deleteById(Vendor vendor);
实体类:
//属性private String vendId;private String vendName;private String vendAddress;private String vendCity;private String vendState;private String vendZip;private String vendCountry;//无参构造器public Vendor() {super();}//带参构造器public Vendor(String vendId, String vendName, String vendAddress, String vendCity, String vendState, String vendZip,String vendCountry) {super();this.vendId = vendId;this.vendName = vendName;this.vendAddress = vendAddress;this.vendCity = vendCity;this.vendState = vendState;this.vendZip = vendZip;this.vendCountry = vendCountry;}//getter、setter方法public String getVendId() {return vendId;}public void setVendId(String vendId) {this.vendId = vendId;}public String getVendName() {return vendName;}public void setVendName(String vendName) {this.vendName = vendName;}public String getVendAddress() {return vendAddress;}public void setVendAddress(String vendAddress) {this.vendAddress = vendAddress;}public String getVendCity() {return vendCity;}public void setVendCity(String vendCity) {this.vendCity = vendCity;}public String getVendState() {return vendState;}public void setVendState(String vendState) {this.vendState = vendState;}public String getVendZip() {return vendZip;}public void setVendZip(String vendZip) {this.vendZip = vendZip;}public String getVendCountry() {return vendCountry;}public void setVendCountry(String vendCountry) {this.vendCountry = vendCountry;}//覆盖toString方法@Overridepublic String toString() {return "Vendor [vendId=" + vendId + ", vendName=" + vendName + ", vendAddress=" + vendAddress + ", vendCity="+ vendCity + ", vendState=" + vendState + ", vendZip=" + vendZip + ", vendCountry=" + vendCountry + "]";}
测试文件:
InputStream inputStream = null;SqlSessionFactory sqlSessionFactory = null;SqlSession session = null;try {inputStream = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);session = sqlSessionFactory.openSession();VendorMapper mapper = session.getMapper(VendorMapper.class);/*Vendor vendorById = mapper.getVendorById("BRE02"); *//*mapper.insertVendor(new Vendor("BER10", "PEOPLE", "CHINA", "GUANGZHOU", "20O", "ABC", "GUANGZ"));*/Vendor v = new Vendor();v.setVendId("BER10");/*v.setVendName("我类个擦");Boolean b = mapper.updateById(v);System.out.println(b);*/Boolean deleteById = mapper.deleteById(v);System.out.println(deleteById);sessionmit();} catch (IOException e) {e.printStackTrace();}finally {try {session.close();inputStream.close();} catch (IOException e) {e.printStackTrace();}}
如果通过mybatis的openSession()手动获取SqlSession的,则需要手动commit或rollback。如果是openSession(true)获取的SQLSession,则表示自动提交,无效手动commit
。与spring整合后mybatis的 SQLSession
创建交给了spring容器来管理,所以就无需手动commit了
本文标签: mybatis 简单增删改
版权声明:本文标题:mybatis: 简单增删改 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732361135h1535248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论