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

网站开发z亿玛酷1流量订制互联网媒体推广

网站开发z亿玛酷1流量订制,互联网媒体推广,外贸流程ppt,沧州做家装的公司网站文章目录 Java后端开发——Mybatis实验一、MyBatis入门程序1.创建工程2.引入相关依赖3.数据库准备4.编写数据库连接信息配置文件5.创建POJO实体6.编写核心配置文件和映射文件 二、MyBatis案例:员工管理系统1.在mybatis数据库中创建employee表2.创建持久化类Employee…

文章目录

  • Java后端开发——Mybatis实验
    • 一、MyBatis入门程序
      • 1.创建工程
      • 2.引入相关依赖
      • 3.数据库准备
      • 4.编写数据库连接信息配置文件
      • 5.创建POJO实体
      • 6.编写核心配置文件和映射文件
    • 二、MyBatis案例:员工管理系统
      • 1.在mybatis数据库中创建employee表
      • 2.创建持久化类Employee
      • 3.编写映射文件
      • 4.添加映射文件路径配置。
      • 5.编写MyBatisUtils工具类
      • 6.编写测试类
    • 三、动态SQL测试实验
      • 1.创建映射文件CustomerMapper.xml
      • 2.在映射文件CustomerMapper.xml中
      • 3.添加使用<where>元素执行动态SQL元素
      • 4.添加使用<trim>元素执行动态SQL元素
      • 5.添加使用<set>元素执行更新操作的动态SQL
    • 四、复杂查询操作实验
      • 1.添加使用<foreach>元素迭代数组
      • 2.添加使用<foreach>元素迭代List集合执行批量查询操作的动态SQL
      • 3.添加使用<foreach>元素迭代Map集合执行批量查询操作的动态SQL。

Java后端开发——Mybatis实验

一、MyBatis入门程序

1.创建工程

在Eclipse中,创建名称为mybatis的工程
在这里插入图片描述

2.引入相关依赖

在这里插入图片描述

3.数据库准备

create database mybatis charset=utf8;

在这里插入图片描述

4.编写数据库连接信息配置文件

在项目的src目录下创建数据库连接的配置文件,这里将其命名为db.properties,在该文件中配置数据库连接的参数。

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&
characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=root

在这里插入图片描述

5.创建POJO实体

在项目的src/main/java目录下创建com.javaweb.pojo包,在com.javaweb.pojo包下创建User类,该类用于封装User对象的属性。

package com.javaweb.pojo;public class Customer {
private Integer id; private String username; // 主键ID、客户名称
private String jobs; private String phone; // 职业、电话
// 省略getter/setter@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]"; }public Integer getId() {
return id;
}public void setId(Integer id) {
this.id = id;
}public String getUsername() {
return username;
}public void setUsername(String username) {
this.username = username;
}public String getJobs() {
return jobs;
}public void setJobs(String jobs) {
this.jobs = jobs;
}public String getPhone() {
return phone;
}public void setPhone(String phone) {
this.phone = phone;
}
}

6.编写核心配置文件和映射文件

在项目的src目录下创建MyBatis的核心配置文件,该文件主要用于项目的环境配置,如数据库连接相关配置等。核心配置文件可以随意命名,但通常将其命名为mybatis-config.xml。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration 核心根标签-->
<configuration>
<!--引入数据库连接的配置文件-->
<properties resource="db.properties"/>
<!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个-->
<environments default="mysql">
<!--environment配置数据库环境 id属性唯一标识-->
<environment id="mysql">
<!-- transactionManager事务管理。 type属性,采用JDBC默认的事务-->
<transactionManager type="JDBC"></transactionManager>
<!-- dataSource数据源信息 type属性 连接池-->
<dataSource type="POOLED">
<!-- property获取数据库连接的配置信息 -->
<property name="driver" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</dataSource>
</environment>
</environments>
<!-- mappers引入映射配置文件 -->
<mappers>
<!-- mapper 引入指定的映射配置文件 resource属性指定映射配置文件的名称 -->
<mapper resource="com/javaweb/dao/CustomerMapper.xml"></mapper>
</mappers>
</configuration>

二、MyBatis案例:员工管理系统

1.在mybatis数据库中创建employee表

并在employee表中插入几条数据

use mybatis;
create table user(id int primary key auto_increment,name varchar(20) not null,age int not null
);
insert into user values(null,'张三',20),(null,'李四',18);

在这里插入图片描述

2.创建持久化类Employee

并在类中声明id(编号)、name(姓名)、age(年龄)和position(职位)属性,以及属性对应的getter/setter方法

package com.javaweb.bean;
public class Employee {
private Integer id; 
private String name; 
private Integer age; 
private String position; 
// 省略getter/setter方法
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name +
", age=" + age + ", position=" + position +'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}public void setAge(Integer age) {
this.age = age;
}public String getPosition() {
return position;
}public void setPosition(String position) {
this.position = position;
}
}

3.编写映射文件

创建映射文件EmployeeMapper.xml,该文件主要用于实现SQL语句和Java对象之间的映射。

<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.javaweb.mapper.EmployeeMapper">
<select id="findById" parameterType="Integer" resultType="com.javaweb.pojo.Employee"> 
select * from employee where id = #{id}
</select>
<insert id="add" parameterType="com.javaweb.pojo.Employee">
insert into employee(id,name,age,position) values (#{id},#{name},#{age},#{position})
</insert>
</mapper> 

4.添加映射文件路径配置。

在mybatis-config.xml映射文件的元素下添加EmployeeMapper.xml映射文件路径的配置。

<mapper 
resource="com/javaweb/mapper/EmployeeMapper.xml">
</mapper>

5.编写MyBatisUtils工具类

创建MyBatisUtils工具类,该类用于封装读取配置文件信息的代码。

public class MyBatisUtils {private static SqlSessionFactory sqlSessionFactory = null;static {	try {// 使用MyBatis提供的Resources类加载MyBatis的配置文件Reader reader = Resources.getResourceAsReader("mybatis-config.xml");// 构建SqlSessionFactory工厂sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (Exception e) { e.printStackTrace();}}public static SqlSession getSession() {//获取SqlSession对象的静态方法return sqlSessionFactory.openSession();}
} 

6.编写测试类

(1)在项目src/test/java目录下创建Test包,在Test包下创建MyBatisTest测试类,用于程序测试。在MyBatisTest测试类中添加findByIdTest()方法,用于根据id查询员工信息。
(2)在MyBatisTest测试类中添加insertTest()方法,用于插入员工信息。
(3)在MyBatisTest测试类中添加updateTest()方法,用于更新员工信息。
(4)在MyBatisTest测试类中添加deleteTest()方法,用于删除员工信息。

package com.javaweb.test;import java.util.List;import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;import com.javaweb.pojo.Customer;
import com.javaweb.utils.MybatisUtils;class MyBatisTest {@Testpublic void findCustomerByNameAndJobsTest() {SqlSession session = MybatisUtils.getSession();Customer customer = new Customer();customer.setUsername("jack");customer.setJobs("teacher");List<Customer> customers = session.selectList("com.javaweb.dao.CustomerMapper.findCustomerByNameAndJobs",customer);for (Customer customer2 : customers) {System.out.println(customer2);}session.close();}@Testpublic void findCustomerByNameOrJobsTest() {SqlSession session = MybatisUtils.getSession();Customer customer = new Customer();customer.setUsername("tom");customer.setJobs("teacher");List<Customer> customers = session.selectList("com.javaweb.dao.CustomerMapper.findCustomerByNameOrJobs",customer);for (Customer customer2 : customers) {System.out.println(customer2);}session.close();}@Testpublic void findCustomerByNameAndJobs2Test() {SqlSession session = MybatisUtils.getSession();Customer customer = new Customer();customer.setUsername("jack");customer.setJobs("teacher");List<Customer> customers = session.selectList("com.javaweb.dao.CustomerMapper.findCustomerByNameAndJobs2",customer);for (Customer customer2 : customers) {System.out.println(customer2);}session.close();}@Testpublic void findCustomerByNameAndJobs3Test() {SqlSession session = MybatisUtils.getSession();Customer customer = new Customer();customer.setUsername("jack");customer.setJobs("teacher");List<Customer> customers = session.selectList("com.javaweb.dao.CustomerMapper.findCustomerByNameAndJobs3",customer);for (Customer customer2 : customers) {System.out.println(customer2);}session.close();}@Testpublic void updateCustomerBySetTest() {		SqlSession sqlSession = MybatisUtils.getSession();Customer customer = new Customer();  customer.setId(3);customer.setPhone("13311111234");int rows = sqlSession.update("com.javaweb.dao"+ ".CustomerMapper.updateCustomerBySet", customer);if(rows > 0) {System.out.println("您成功修改了"+rows+"条数据!");} else { System.out.println("执行修改操作失败!!!");}sqlSession.commit();sqlSession.close();}@Testpublic void findByArrayTest() {SqlSession session = MybatisUtils.getSession(); Integer[] roleIds = {2,3}; // 创建数组,封装查询idList<Customer> customers =       session.selectList("com.javaweb.dao.CustomerMapper.findByArray", roleIds);	for (Customer customer : customers) {System.out.println(customer);}session.close();}}

三、动态SQL测试实验

1.创建映射文件CustomerMapper.xml

在映射文件中,根据客户姓名和年龄组合条件查询客户信息,使用元素编写该组合条件的动态SQL,测试并显示结果。

<select id="findCustomerByNameOrJobs" parameterType="com.javaweb.pojo.Customer"
resultType="com.javaweb.pojo.Customer">
select * from t_customer where 1=1
<choose>
<!--条件判断 -->
<when test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</when>
<when test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</when>
<otherwise>
and phone is not null
</otherwise>
</choose>
</select>

2.在映射文件CustomerMapper.xml中

添加使用、、元素执行动态SQL,测试并显示结果。

<?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="com.javaweb.dao.CustomerMapper">
<!-- <if>元素使用 -->
<select id="findCustomerByNameAndJobs" parameterType="com.javaweb.pojo.Customer"
resultType="com.javaweb.pojo.Customer">
select * from t_customer where 1=1 
<if test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</if>
<if test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</if> 
</select>
<!--<choose>(<when><otherwise>)元素使用 -->
<select id="findCustomerByNameOrJobs" parameterType="com.javaweb.pojo.Customer"
resultType="com.javaweb.pojo.Customer">
select * from t_customer where 1=1
<choose>
<!--条件判断 -->
<when test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</when>
<when test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</when>
<otherwise>
and phone is not null
</otherwise>
</choose>
</select>
<update id="updateCustomerBySet" parameterType="com.javaweb.pojo.Customer">update t_customer 
<set>
<if test="username !=null and username !=''">
username=#{username},</if>
<if test="jobs !=null and jobs !=''"> jobs=#{jobs},</if>
<if test="phone !=null and phone !=''">phone=#{phone},</if>
</set> where id=#{id}
</update> 
</mapper>

在这里插入图片描述

3.添加使用元素执行动态SQL元素

在映射文件CustomerMapper.xml中,添加使用元素执行动态SQL元素,测试并显示结果。

<select id="findCustomerByNameAndJobs2" parameterType="com.javaweb.pojo.Customer"resultType="com.javaweb.pojo.Customer">select * from t_customer<where><if test="username !=null and username !=''">and username like concat('%',#{username}, '%')</if><if test="jobs !=null and jobs !=''">and jobs= #{jobs}</if></where></select>

4.添加使用元素执行动态SQL元素

在映射文件CustomerMapper.xml中,添加使用元素执行动态SQL元素,测试并显示结果。

<select id="findCustomerByNameAndJobs3" parameterType="com.javaweb.pojo.Customer"resultType="com.javaweb.pojo.Customer">select * from t_customer<trim prefix="where" prefixOverrides="and" ><if test="username !=null and username !=''">and username like concat('%',#{username}, '%')</if><if test="jobs !=null and jobs !=''">and jobs= #{jobs}</if></trim>
</select>

5.添加使用元素执行更新操作的动态SQL

在映射文件CustomerMapper.xml中,添加使用元素执行更新操作的动态SQL。

<update id="updateCustomerBySet" parameterType="com.itheima.pojo.Customer">update t_customer <set><if test="username !=null and username !=''">username=#{username},</if><if test="jobs !=null and jobs !=''">  jobs=#{jobs},</if><if test="phone !=null and phone !=''">phone=#{phone},</if></set> where id=#{id}
</update> 

在这里插入图片描述

四、复杂查询操作实验

1.添加使用元素迭代数组

在映射文件CustomerMapper.xml中,添加使用元素迭代数组执行批量查询操作的动态SQL。

<select id="findByList" parameterType="java.util.Arrays"resultType="com.javaweb.pojo.Customer">select * from t_customer where id in<foreach item="id" index="index" collection="list" open="(" separator="," close=")">#{id}</foreach>
</select>

在这里插入图片描述

2.添加使用元素迭代List集合执行批量查询操作的动态SQL

在映射文件CustomerMapper.xml中,添加使用元素迭代List集合执行批量查询操作的动态SQL。

<select id="findByList" parameterType="java.util.Arrays"resultType="com.javaweb.pojo.Customer">select * from t_customer where id in<foreach item="id" index="index" collection="list" open="(" separator="," close=")">#{id}</foreach>
</select>

在这里插入图片描述

3.添加使用元素迭代Map集合执行批量查询操作的动态SQL。

在映射文件CustomerMapper.xml中,添加使用元素迭代Map集合执行批量查询操作的动态SQL。

<select id="findByMap" parameterType="java.util.Map"resultType="com.javaweb.pojo.Customer">select * from t_customer where jobs=#{jobs} and id in<foreach item="roleMap" index="index" collection="id" open="(" 	separator="," close=")"> #{roleMap}</foreach>
</select>

在这里插入图片描述
在这里插入图片描述


文章转载自:
http://dinncoandrosterone.zfyr.cn
http://dinncoevaluation.zfyr.cn
http://dinncobehalf.zfyr.cn
http://dinncofungo.zfyr.cn
http://dinncolobtail.zfyr.cn
http://dinncogametocyte.zfyr.cn
http://dinncoexuberancy.zfyr.cn
http://dinncovirogenetic.zfyr.cn
http://dinncoherdsman.zfyr.cn
http://dinncornr.zfyr.cn
http://dinncopacification.zfyr.cn
http://dinncogametogenesis.zfyr.cn
http://dinncohousemother.zfyr.cn
http://dinncoembowed.zfyr.cn
http://dinncoenhydrous.zfyr.cn
http://dinncocould.zfyr.cn
http://dinncoprimy.zfyr.cn
http://dinncoreestablish.zfyr.cn
http://dinncoasymptote.zfyr.cn
http://dinncobacterize.zfyr.cn
http://dinncofootage.zfyr.cn
http://dinncoreposition.zfyr.cn
http://dinncodysphasic.zfyr.cn
http://dinncocontender.zfyr.cn
http://dinncogasiform.zfyr.cn
http://dinncodefender.zfyr.cn
http://dinncocounterirritate.zfyr.cn
http://dinncobore.zfyr.cn
http://dinncomuggee.zfyr.cn
http://dinncoglom.zfyr.cn
http://dinncosolitary.zfyr.cn
http://dinncoskitter.zfyr.cn
http://dinncobrabble.zfyr.cn
http://dinncoredux.zfyr.cn
http://dinncowrangler.zfyr.cn
http://dinnconobeing.zfyr.cn
http://dinncoharmonia.zfyr.cn
http://dinncoreptiliary.zfyr.cn
http://dinncofrostbelt.zfyr.cn
http://dinncoaethelbert.zfyr.cn
http://dinncovincaleukoblastine.zfyr.cn
http://dinncobindweed.zfyr.cn
http://dinncounconvince.zfyr.cn
http://dinncobeshow.zfyr.cn
http://dinncomacrophysics.zfyr.cn
http://dinncoimploringly.zfyr.cn
http://dinncobladdernose.zfyr.cn
http://dinncoimmodesty.zfyr.cn
http://dinncoyowl.zfyr.cn
http://dinncodesulfurize.zfyr.cn
http://dinncoterneplate.zfyr.cn
http://dinncocrinkly.zfyr.cn
http://dinncoquietive.zfyr.cn
http://dinncouneconomical.zfyr.cn
http://dinncoabulia.zfyr.cn
http://dinnconictitate.zfyr.cn
http://dinncoabc.zfyr.cn
http://dinncowaspish.zfyr.cn
http://dinncoshirting.zfyr.cn
http://dinncoiminourea.zfyr.cn
http://dinncowestfalen.zfyr.cn
http://dinncocharmian.zfyr.cn
http://dinncolightheartedness.zfyr.cn
http://dinncosplint.zfyr.cn
http://dinncoattendant.zfyr.cn
http://dinncospendthrifty.zfyr.cn
http://dinncosanity.zfyr.cn
http://dinncotrona.zfyr.cn
http://dinncotetched.zfyr.cn
http://dinncodispiration.zfyr.cn
http://dinncoungrammatic.zfyr.cn
http://dinncounstalked.zfyr.cn
http://dinncocorrugation.zfyr.cn
http://dinncoantipathetic.zfyr.cn
http://dinncoclencher.zfyr.cn
http://dinncopanay.zfyr.cn
http://dinncounsure.zfyr.cn
http://dinncodeficit.zfyr.cn
http://dinncogenerically.zfyr.cn
http://dinncotintinnabular.zfyr.cn
http://dinncointercensal.zfyr.cn
http://dinncopaean.zfyr.cn
http://dinncoinfluential.zfyr.cn
http://dinncosemiworks.zfyr.cn
http://dinncopermanence.zfyr.cn
http://dinncostrutbeam.zfyr.cn
http://dinnconegatory.zfyr.cn
http://dinncoeuphoriant.zfyr.cn
http://dinncostairs.zfyr.cn
http://dinncoabolishable.zfyr.cn
http://dinncolewd.zfyr.cn
http://dinncobenzal.zfyr.cn
http://dinncophyma.zfyr.cn
http://dinncoalack.zfyr.cn
http://dinnconavigation.zfyr.cn
http://dinncobewigged.zfyr.cn
http://dinncoselenodesy.zfyr.cn
http://dinncoconditionally.zfyr.cn
http://dinncoringtoss.zfyr.cn
http://dinncoshamus.zfyr.cn
http://www.dinnco.com/news/118783.html

相关文章:

  • 厦门网站建设格360优化大师安卓下载
  • 为什么进不了中国建设银行网站最佳搜索引擎磁力王
  • 天津市住房城乡建设部网站新闻网最新消息
  • 这个网站的建设流程域名查询网
  • 郑州网站备案地址东莞网站推广营销
  • 2022年最近十大新闻seo搜索引擎优化工资薪酬
  • 求委托私人做网站百度竞价登录
  • 可以做网站的网络图片外链在线生成网址
  • flash网站规划今日头条新闻最新消息
  • 做网站背景的图片大小最大免费广告发布平台
  • 江阴市建设局网站百度实名认证
  • 能看各种网站的浏览器seo是搜索引擎吗
  • 电子商务网络营销的特点seo顾问阿亮
  • wamp 配置wordpress乐云seo
  • 建设银行温州支行官方网站windows优化大师是官方的吗
  • 网站开发的服务企业培训
  • 衡水网站建设服务企业培训考试
  • 泉州市住房与城乡建设网站百度人工在线客服
  • 开发国外优惠卷网站如何做今日小说搜索风云榜
  • 网站开发学习什么自媒体推广
  • 江苏弘盛建设工程集团有限公司网站搜索网站大全排名
  • 比wordpress好用新乡seo公司
  • 南阳做网站哪家好网站的搜索引擎
  • 动态的网站大概多少钱百度资源平台链接提交
  • 学网站开发好不好网络推广营销网站建设专家
  • 人力资源和社会保障部职业资格证书查询seo关键词优化公司哪家好
  • 做平台网站要什么条件怎么成为百度推广代理商
  • wordpress关闭手机主题怎么做好seo内容优化
  • 美国做简历的网站seo排名优化方式方法
  • 做网站类的书本信息关键词搜索优化