网站seo博客做网站需要学编程吗
- 作者: 五速梦信息网
- 时间: 2026年04月20日 08:14
当前位置: 首页 > news >正文
网站seo博客,做网站需要学编程吗,如何查询营业执照注册信息,广告公司做的网站字体侵权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之环境搭建以及实现增删改查 实现步骤
- 编写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); }
- 编写测试类
- 执行步骤 读取MyBatis配置文件实例化SqlSessionFactory实例化SqlSession获取PersonMapper接口 通过session.getMapper(PersonMapper.class)的方式获取接口执行SQL增删改的场合完成数据提交
- 代码实例 完成对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();}}}
- 运行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的传统方式和面向接口编程方式完成数据库的增删改查大家可以动手试试欢迎留言交流下篇见。
- 上一篇: 网站seo报告旅游网站建设内容
- 下一篇: 网站seo策划广州h5网站建设公司
相关文章
-
网站seo报告旅游网站建设内容
网站seo报告旅游网站建设内容
- 技术栈
- 2026年04月20日
-
网站seo 优化微盟微商城电商小程序
网站seo 优化微盟微商城电商小程序
- 技术栈
- 2026年04月20日
-
网站seo 优化帮别人做网站怎么接单
网站seo 优化帮别人做网站怎么接单
- 技术栈
- 2026年04月20日
-
网站seo策划广州h5网站建设公司
网站seo策划广州h5网站建设公司
- 技术栈
- 2026年04月20日
-
网站seo策略企业宣传片拍摄思路
网站seo策略企业宣传片拍摄思路
- 技术栈
- 2026年04月20日
-
网站seo服务公司网站开发的教学网站
网站seo服务公司网站开发的教学网站
- 技术栈
- 2026年04月20日
