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

17.zwd一起做网站代运营套餐价格表

17.zwd一起做网站,代运营套餐价格表,张家口人社app最新下载,做网站标签栏的图片大小MyBatis之环境搭建以及实现增删改查 前言实现步骤1. 编写MyBatis-config.xml配置文件2. 编写Mapper.xml文件(增删改查SQL文)3. 定义PeronMapper接口4. 编写测试类1. 执行步骤2. 代码实例3. 运行log 开发环境构造图总结 前言 上一篇文章,我们…

MyBatis之环境搭建以及实现增删改查

  • 前言
  • 实现步骤
    • 1. 编写MyBatis-config.xml配置文件
    • 2. 编写Mapper.xml文件(增删改查SQL文)
    • 3. 定义PeronMapper接口
    • 4. 编写测试类
      • 1. 执行步骤
      • 2. 代码实例
      • 3. 运行log
  • 开发环境构造图
  • 总结

前言

上一篇文章,我们使用MyBatis传统的方式(namespace+id,非接口式编程),完成了数据库的增删改查操作,今天我们学习动态代理的方式(面向接口编程),简单的说,就是将Mapper.xml(定义数据库增删改查SQL文的文件)中定义的SQLID以方法的形式定义在Interface中,调用其方法,完成数据的增删改查,这种方法,也是大部分项目中使用的方法,环境的搭建和准备工作,还是参看我的上一篇文章。
MyBatis之环境搭建以及实现增删改查


实现步骤

1. 编写MyBatis-config.xml配置文件

编写配置文件,上一篇文章也有介绍,
今天学习两个新功能,第一个就是编写单独的数据库信息文件,在配置文件中读取后,使用${}方式,读取文件中的内容,配置数据库信息,防止硬编码,完成数据库信息统一管理。
首先编写db.properties,将数据库信息定义在此文件中,通常该文件放在classpath下。
示例代码,如下:

my.driver=com.mysql.cj.jdbc.Driver
my.url=jdbc:mysql://192.168.56.88:3306/mysql
my.username=root
my.password=root

在使用< properties>标签,引入到MyBatis-config.xml配置文件中

第二个就是简化参数的写法,上一篇文章,我们看到Mapper.xml文件中,定义SQL的id时,如果输入参数是JavaBean,都是以全类名的形式配置的,这样代码比较冗余,我们使用< typeAliases>< package>标签,批量引用JavaBean,SQL输入输出参数时,可以省略包名的形式,直接使用JavaBean的类名。

MyBatis-config.xml代码如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 读取外部数据库信息文件 --><properties resource="db.properties" /><!-- 设置JavaBean类型的参数别名 --><typeAliases><package name="xxx.xxx.pojo"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${my.driver}"/><property name="url" value="${my.url}"/><property name="username" value="${my.username}"/><property name="password" value="${my.password}"/></dataSource></environment></environments><mappers><mapper resource="xxx/xxx/mapper/PersonMapper.xml"/></mappers>
</configuration>

2. 编写Mapper.xml文件(增删改查SQL文)

编写增删改查的SQL文,这里就不做展开了,需要注意的是,我们已经在配置文件中批量引入JavaBean,我们只需使用类名(person)即可,通常类名开头小写。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.entry.personMapper"><select id="queryAllPerson" resultType="person">select * from person</select><select id="queryPersonById" resultType="person" parameterType="int">select * from person where id = #{id}</select><insert id="addPerson" parameterType="person">insert into person values(#{id}, #{name}, #{age}, #{sex})</insert><update id="updatePersonById" parameterType="person">update person set name = #{name}, age = #{age}, sex = #{sex} where id = #{id}</update><delete id="deletePersonById" parameterType="int">delete from  person where id = #{id}</delete>
</mapper>

3. 定义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 List<Person> queryAllPerson();public Person queryPersonById(int id);public int addPerson(Person person);public int updatePersonById(Person person);public int deletePersonById(int id);
}

4. 编写测试类

1. 执行步骤

  1. 读取MyBatis配置文件
  2. 实例化SqlSessionFactory
  3. 实例化SqlSession
  4. 获取PersonMapper接口
    通过session.getMapper(PersonMapper.class)的方式,获取接口
  5. 执行SQL
  6. 增删改的场合,完成数据提交

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.执行SQLList<Person> 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.执行SQLList<Person> 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 [id=1, name=zs, age=23]
更新前
Person [id=1, name=zs, age=23]
更新件数:1
更新后
Person [id=1, name=ls, age=24]
删除前
Person [id=1, name=ls, age=24]
删除件数:1
删除后
null

开发环境构造图

在这里插入图片描述

总结

到这里,我们就完成了MyBatis的传统方式和面向接口编程方式完成数据库的增删改查,大家可以动手试试,欢迎留言交流,下篇见。

http://www.yidumall.com/news/17395.html

相关文章:

  • wordpress邮箱社交郑州网站seo顾问
  • 东乡哪里有做网站网络广告类型
  • 做业务不花钱的网站有哪些石家庄seo按天扣费
  • wordpress不锈钢企业丽水百度seo
  • 班级网站成品最新军事动态最新消息
  • 免费域名注册和免费建站站长工具综合查询系统
  • 做网站分什么精准营销及推广
  • 广州一次做网站百度电话人工服务
  • 西安做网站哪家便宜seo专业技术培训
  • 上海的做网站的公司优化网站怎么真实点击
  • 德阳做网站的平面设计
  • 网站的免费空间是什么seo网站推广方式
  • 政府网站建设问题淘宝推广费用一般多少
  • wordpress 自定义php太原seo招聘
  • 自己网站做seo广州引流推广公司
  • 微信 公司网站 怎么做白度
  • 桂林遇龙河聊城优化seo
  • 营销型企业网站诊断百度一下你就知道移动首页
  • seo 工具推荐seo搜索引擎招聘
  • wordpress标题字体改大淘宝优化标题都是用什么软件
  • 二级域名ip查询深圳搜索引擎优化收费
  • 域名及网站建设实训西地那非
  • 个人网站建设方案书 备案索引擎优化 seo
  • 天津网站制作计划快速提升排名seo
  • 目前最新国际消息郑州搜狗关键词优化顾问
  • 翻译网站怎么做怎么做公众号
  • 福州网站开发私人百度推广是什么
  • 网站的运营百度指数查询工具app
  • 物流营销型网站案例搜索引擎优化的基本方法
  • 做承诺的网站软文关键词排名推广