当前位置: 首页 > news >正文

免费的行情网站英文网站建设网站

免费的行情网站,英文网站建设网站,镇江润州区建设局网站,外贸展示网站多少钱MyBatis之环境搭建以及实现增删改查 前言实现步骤1. 编写MyBatis-config.xml配置文件2. 编写Mapper.xml文件#xff08;增删改查SQL文#xff09;3. 定义PeronMapper接口4. 编写测试类1. 执行步骤2. 代码实例3. 运行log 开发环境构造图总结 前言 上一篇文章#xff0c;我们… MyBatis之环境搭建以及实现增删改查 前言实现步骤1. 编写MyBatis-config.xml配置文件2. 编写Mapper.xml文件增删改查SQL文3. 定义PeronMapper接口4. 编写测试类1. 执行步骤2. 代码实例3. 运行log 开发环境构造图总结 前言 上一篇文章我们使用MyBatis传统的方式namespaceid非接口式编程完成了数据库的增删改查操作今天我们学习动态代理的方式面向接口编程简单的说就是将Mapper.xml定义数据库增删改查SQL文的文件中定义的SQLID以方法的形式定义在Interface中调用其方法完成数据的增删改查这种方法也是大部分项目中使用的方法环境的搭建和准备工作还是参看我的上一篇文章。 MyBatis之环境搭建以及实现增删改查 实现步骤 1. 编写MyBatis-config.xml配置文件 编写配置文件上一篇文章也有介绍 今天学习两个新功能第一个就是编写单独的数据库信息文件在配置文件中读取后使用${}方式读取文件中的内容配置数据库信息防止硬编码完成数据库信息统一管理。 首先编写db.properties将数据库信息定义在此文件中通常该文件放在classpath下。 示例代码如下: my.drivercom.mysql.cj.jdbc.Driver my.urljdbc:mysql://192.168.56.88:3306/mysql my.usernameroot my.passwordroot在使用 properties标签引入到MyBatis-config.xml配置文件中 第二个就是简化参数的写法上一篇文章我们看到Mapper.xml文件中定义SQL的id时如果输入参数是JavaBean都是以全类名的形式配置的这样代码比较冗余我们使用 typeAliases package标签批量引用JavaBeanSQL输入输出参数时可以省略包名的形式直接使用JavaBean的类名。 MyBatis-config.xml代码如下 ?xml version1.0 encodingUTF-8 ? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configuration!-- 读取外部数据库信息文件 --properties resourcedb.properties /!-- 设置JavaBean类型的参数别名 --typeAliasespackage namexxx.xxx.pojo//typeAliasesenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver value${my.driver}/property nameurl value${my.url}/property nameusername value${my.username}/property namepassword value${my.password}//dataSource/environment/environmentsmappersmapper resourcexxx/xxx/mapper/PersonMapper.xml//mappers /configuration2. 编写Mapper.xml文件增删改查SQL文 编写增删改查的SQL文这里就不做展开了需要注意的是我们已经在配置文件中批量引入JavaBean我们只需使用类名person即可通常类名开头小写。 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacexxx.xxx.entry.personMapperselect idqueryAllPerson resultTypepersonselect * from person/selectselect idqueryPersonById resultTypeperson parameterTypeintselect * from person where id #{id}/selectinsert idaddPerson parameterTypepersoninsert into person values(#{id}, #{name}, #{age}, #{sex})/insertupdate idupdatePersonById parameterTypepersonupdate person set name #{name}, age #{age}, sex #{sex} where id #{id}/updatedelete iddeletePersonById parameterTypeintdelete from person where id #{id}/delete /mapper3. 定义PeronMapper接口 将Mapper.xml中SQL的id定义到PeronMapper接口中输入参数和输出参数与SQL的id中的保持一致。 这里需要注意的是接口文件和Mapper.xml放到同一个文件中文件名保持一致。 package xxx.xxx.mapper;import java.util.List;import xxx.xxx.pojo.Person;public interface PersonMapper {public ListPerson queryAllPerson();public Person queryPersonById(int id);public int addPerson(Person person);public int updatePersonById(Person person);public int deletePersonById(int id); } 4. 编写测试类 1. 执行步骤 读取MyBatis配置文件实例化SqlSessionFactory实例化SqlSession获取PersonMapper接口 通过session.getMapper(PersonMapper.class)的方式获取接口执行SQL增删改的场合完成数据提交 2. 代码实例 完成对Person表的增删改查 package xxx.xxx.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 xxx.xxx.mapper.PersonMapper; import xxx.xxx.pojo.Person;public class TestMyBatis {public static void main(String[] args) throws IOException {Person person new Person(1, zs, 23, true);System.err.println(登录前);queryPersonById(1);addPerson(person);System.err.println(登录后);queryPersonById(1);person new Person(1, ls, 24, true);System.err.println(更新前);queryAllPerson();updatePersonById(person);System.err.println(更新后);queryPersonById(1);System.err.println(删除前);queryPersonById(1);deletePersonById(1);System.err.println(删除后);queryPersonById(1);}public static void queryAllPersonUsePersonMapping() throws IOException {// 1.读取MyBatis配置文件Reader reader Resources.getResourceAsReader(mybatis-config.xml);// 2.实例化SqlSessionFactorySqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(reader);// 3.实例化SqlSessiontry (SqlSession session sqlSessionFactory.openSession()) {// 4.获取PersonMapper接口PersonMapper personMapper session.getMapper(PersonMapper.class);// 5.执行SQLListPerson persons personMapper.queryAllPerson();persons.forEach(System.err::println);}}public static void queryAllPerson() throws IOException {// 1.读取MyBatis配置文件Reader reader Resources.getResourceAsReader(mybatis-config.xml);// 2.实例化SqlSessionFactorySqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(reader);// 3.实例化SqlSessiontry (SqlSession session sqlSessionFactory.openSession()) {// 4.获取PersonMapper接口PersonMapper personMapper session.getMapper(PersonMapper.class);// 5.执行SQLListPerson persons personMapper.queryAllPerson();persons.forEach(System.err::println);}}public static void queryPersonById(int id) throws IOException {// 1.读取MyBatis配置文件Reader reader Resources.getResourceAsReader(mybatis-config.xml);// 2.实例化SqlSessionFactorySqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(reader);// 3.实例化SqlSessiontry (SqlSession session sqlSessionFactory.openSession()) {// 4.获取PersonMapper接口PersonMapper personMapper session.getMapper(PersonMapper.class);// 5.执行SQLPerson person personMapper.queryPersonById(1);System.err.println(person);}}public static void addPerson(Person person) throws IOException {// 1.读取MyBatis配置文件Reader reader Resources.getResourceAsReader(mybatis-config.xml);// 2.实例化SqlSessionFactorySqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(reader);// 3.实例化SqlSessiontry (SqlSession session sqlSessionFactory.openSession()) {// 4.获取PersonMapper接口PersonMapper personMapper session.getMapper(PersonMapper.class);// 5.执行SQLint count personMapper.addPerson(person);System.err.println(登陆件数 count);// 6.增删改的场合完成数据提交session.commit();}}public static void updatePersonById(Person person) throws IOException {// 1.读取MyBatis配置文件Reader reader Resources.getResourceAsReader(mybatis-config.xml);// 2.实例化SqlSessionFactorySqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(reader);// 3.实例化SqlSessiontry (SqlSession session sqlSessionFactory.openSession()) {// 4.获取PersonMapper接口PersonMapper personMapper session.getMapper(PersonMapper.class);// 5.执行SQLint count personMapper.updatePersonById(person);System.err.println(更新件数 count);// 6.增删改的场合完成数据提交session.commit();}}public static void deletePersonById(int id) throws IOException {// 1.读取MyBatis配置文件Reader reader Resources.getResourceAsReader(mybatis-config.xml);// 2.实例化SqlSessionFactorySqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(reader);// 3.实例化SqlSessiontry (SqlSession session sqlSessionFactory.openSession()) {// 4.获取PersonMapper接口PersonMapper personMapper session.getMapper(PersonMapper.class);// 5.执行SQLint count personMapper.deletePersonById(id);System.err.println(删除件数 count);// 6.增删改的场合完成数据提交session.commit();}}} 3. 运行log 通过下边的运行log可以看出完成对Person表的增删改查 登录前 null 登陆件数1 登录后 Person [id1, namezs, age23] 更新前 Person [id1, namezs, age23] 更新件数1 更新后 Person [id1, namels, age24] 删除前 Person [id1, namels, age24] 删除件数1 删除后 null开发环境构造图 总结 到这里我们就完成了MyBatis的传统方式和面向接口编程方式完成数据库的增删改查大家可以动手试试欢迎留言交流下篇见。
http://www.w-s-a.com/news/20056/

相关文章:

  • 金融交易网站建设金融 网站建设
  • 长沙网站建设联系电话怎么做表格
  • 网站怎么做域名实名认证龙华网站 建设信科网络
  • 企业网站规划方案网站是做排行榜
  • 万维网网站个人申请网站
  • 我想做网站怎么做昆山网站建设 全是乱码
  • 单位做网站怎么做圣诞树html网页代码
  • 网页开发与网站开发企业网站托管服务常用指南
  • 一站式服务图片临沂做进销存网站
  • 鸣蝉智能建站标准物质网站建设模板
  • 电商网站建设技术员的工作职责商业网站制作价格
  • 网站html模板免费下载公司的网站建设费用入什么科目
  • 高中生做网站网页网页制作教程零基础学会
  • 做金融网站有哪些要求WordPress站内搜索代码
  • 济南网站怎么做seowordpress注册发邮件
  • 珠海网站设计平台东莞市手机网站建设平台
  • 网站开发文档合同怎么在wordpress导航条下方加入文字广告链接
  • 网站建设需怎么做有网站怎么做企业邮箱
  • 网站制作流程视频教程小程序多少钱一年
  • 暗网是什么网站花都网站建设哪家好
  • 贵州网站开发流程晋江论坛手机版
  • 网站建设丿金手指谷哥14阿里巴巴官网电脑版
  • 网站开发招聘信息匿名ip访问网站受限
  • 网站转app工具网站规划建设与管理维护大作业
  • flash是怎么做网站的.net购物网站开发
  • 烟台网站建设求职简历品质商城网站建设
  • 做百度外链哪些网站权重高点做网站具备的条件
  • 怎么样用ppt做网站红番茄 网站点评
  • 建设银行河北分行招聘网站哪里能找到网站
  • 兰州营销型网站网站建设收费标准