当前位置: 首页> 财经> 金融 > 软文营销平台_微信运营模式_小网站怎么搜关键词_肇庆疫情最新消息

软文营销平台_微信运营模式_小网站怎么搜关键词_肇庆疫情最新消息

时间:2025/7/11 15:38:50来源:https://blog.csdn.net/m0_56369671/article/details/144670984 浏览次数:0次
软文营销平台_微信运营模式_小网站怎么搜关键词_肇庆疫情最新消息

0.1 Maven

Maven是apache提供的一个项目管理工具,它的作用就是管理项目
3个作用

1.依赖管理

依赖管理主要指的是项目怎样引入依赖包,maven会将所有需要的依赖包放在本地仓库中,然后每个项目通过配置文件引入自己所需要的那部分

2.统一项目结构

在项目开发中,当你使用不同的开发工具 (如:Eclipse、Idea),创建项目工程时:

 3.项目构建

maven提供了标准的、跨平台(Linux、Windows、MacOS) 的自动化项目构建方式

 3套生命周期

  • clean:清理工作。

  • default:核心工作。如:编译、测试、打包、安装、部署等。

  • site:生成报告、发布站点等。

生命周期的顺序是:clean --> validate --> compile --> test --> package --> verify --> install --> site --> deploy

我们需要关注的就是:clean --> compile --> test --> package --> install

说明:在同一套生命周期中,我们在执行后面的生命周期时,前面的生命周期都会执行

 1.1 mybatis

框架可以理解为半成品软件,框架做好以后,程序员只需要在它基础上继续进行后面的开发

框架中一般都是所有程序员都需要开发的重复代码,框架将这些做好之后,程序员就可以把精力专注在核心业务上

==学习框架, 关注点: 怎么去用这个框架==

Mybatis

MyBatis是一款优秀的持久层框架,使用Mybatis可以轻松的实现Java程序向数据库发送SQL语句

而且对于SQL查询回来的结果进行方便的封装

1. 入门案例

1 准备数据

create database mybatis;
use mybatis;create table user(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age tinyint unsigned comment '年龄',gender tinyint unsigned comment '性别, 1:男, 2:女',phone varchar(11) comment '手机号'
) comment '用户表';insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');

 2. 创建工程,添加依赖

    <dependencies><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!--mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.26</version></dependency></dependencies>

 3. 创建实体类,Mapper接口

4.添加配置文件 

<?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><settings><!--在控制台输出发送的sql日志--><setting name="logImpl" value="STDOUT_LOGGING"/></settings><environments default="development"><environment id="development"><transactionManager type="JDBC"/><!--目前只关注这部分内容,它的作用就是声明要连接的数据信息--><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><!--声明含有sql的接口所在包--><package name="com.itheima.mapper"/></mappers>
</configuration>

 5. 测试

package com.itheima.test;import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
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 org.junit.Test;import java.io.IOException;
import java.io.InputStream;public class UserMapperTest {@Testpublic void testSave() throws IOException {//0. 准备一个User对象User user = new User();user.setName("黑马");user.setAge(18);user.setGender(1);user.setPhone("13800138000");//1. 使用mybatis将user对象保存到数据库(步骤不重要, 不用记)//1-1 读取配置文件,读成一个输入流InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");//1-2 创建SqlSessionFactory对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//1-3 获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//1-4 获取UserMapper对象,调用方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);userMapper.save(user);//1-5 提交事务sqlSession.commit();//1-6 释放资源sqlSession.close();}
}

 2. mybatis增删改

Mybatis使用两步骤:

  1. 在接口中添加方法和sql语句

  2. 在测试类中添加测试方法

2.1 增加

//操作那个表就对应的起名 xxxMapper, 注意这是个接口
public interface UserMapper {//声明一个保存用户的方法, 然后在方法上使用@Insert声明方法对应的sql语句//后面只要一执行方法,mybatis就会帮我们把sql语句发送到数据库中去//#{}中的内容//1. 当方法参数是一个对象时, #{}里面写的是对象中的属性名//主键返回//useGeneratedKeys=true  告诉mybatis,当执行完保存操作之后,需要将数据库中新增记录的主键查询回来//keyProperty = "id"      查询回来的主键值需要封装到方法参数对象的哪个属性上@Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into user values (null,#{name},#{age},#{gender},#{phone})")void save(User user);
}

public class UserMapperTest {@Testpublic void testSave() throws IOException {//0. 准备一个User对象User user = new User();user.setName("黑马");user.setAge(18);user.setGender(1);user.setPhone("13800138000");//1. 使用mybatis将user对象保存到数据库(步骤不重要, 不用记)//1-1 读取配置文件,读成一个输入流InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");//1-2 创建SqlSessionFactory对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//1-3 获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//1-4 获取UserMapper对象,调用方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);userMapper.save(user);//保存之后,打印出user中得idSystem.out.println(user);//1-5 提交事务sqlSession.commit();//1-6 释放资源sqlSession.close();}
}

2.2 修改

public interface UserMapper {//根据id更新@Update("update user set name = #{name},age = #{age}, gender = #{gender},phone=#{phone} where id = #{id} ")void update(User user);
}
@Test
public void testUpdate() throws IOException {//1. 使用mybatis将user对象保存到数据库(步骤不重要, 不用记)//1-1 读取配置文件,读成一个输入流InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");//1-2 创建SqlSessionFactory对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//1-3 获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//1-4 获取UserMapper对象,调用方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);//准备一个User对象,必须包含idUser user = new User();user.setName("李四");user.setAge(20);user.setGender(1);user.setPhone("13700137000");user.setId(1);userMapper.update(user);//1-5 提交事务sqlSession.commit();//1-6 释放资源sqlSession.close();
}

   2.3  删除

//#{}中的内容
//1. 当方法参数是一个对象时, #{}里面写的是对象中的属性名
//2. 当方法参数是一个简单类型参数(8种基本 + 8种包装 + Stringg)时, #{}里面可以随便写,但是推荐写方法形参
//删除
@Delete("delete from user where id = #{id}")
void delete(Integer id);

3. 抽取工具类

package com.itheima.util;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 java.io.InputStream;//mybatis工具类
public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory;//保证SqlSessionFactory是单例的static {try {//1. 读取主配置文件,读成数据流InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");//2. 创建SqlSessionFactorysqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}catch (Exception e){e.printStackTrace();}}//获取SqlSessionpublic static SqlSession getSqlSession() {//3. 获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();return sqlSession;}//提交事务和关闭Sessionpublic static void close(SqlSession sqlSession) {if (sqlSession != null) {//5. 提交事务sqlSession.commit();//6. 释放资源sqlSession.close();}}
}
package com.itheima.test;import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;public class UserMapperTest {@Testpublic void testSave() {//获取sqlSessionSqlSession sqlSession = MyBatisUtil.getSqlSession();//获取UserMapper对象,调用方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);//准备一个User对象,保存User user = new User();user.setName("黑马");user.setAge(18);user.setGender(1);user.setPhone("13800138000");userMapper.save(user);//保存之后,打印出user中得idSystem.out.println(user);//提交事务,释放资源MyBatisUtil.close(sqlSession);}@Testpublic void testUpdate() {//获取sqlSessionSqlSession sqlSession = MyBatisUtil.getSqlSession();//获取UserMapper对象,调用方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);//准备一个User对象,必须包含idUser user = new User();user.setName("李四");user.setAge(20);user.setGender(1);user.setPhone("13700137000");user.setId(1);userMapper.update(user);//提交事务,释放资源MyBatisUtil.close(sqlSession);}@Testpublic void testDelete() {//获取sqlSessionSqlSession sqlSession = MyBatisUtil.getSqlSession();//获取UserMapper对象,调用方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);userMapper.delete(1);//提交事务,释放资源MyBatisUtil.close(sqlSession);}
}

4. mybatis查询

4.1 准备数据

-- 员工管理
create table emp (id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用户名',password varchar(32) default '123456' comment '密码',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',image varchar(300) comment '图像',job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',entrydate date comment '入职时间',dept_id int unsigned comment '部门ID',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间'
) comment '员工表';INSERT INTO emp(id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES(1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),(2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),(3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),(4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),(5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),(6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),(7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),(8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),(9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),(10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),(11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,now(),now()),(12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,now(),now()),(13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,now(),now()),(14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,now(),now()),(15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),(16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2010-01-01',2,now(),now()),(17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());

4.2 结果封装

执行查询SQL是会有结果集返回来的,mybatis会将结果封装到我们指定的实体中

  • 实体类属性名 和 数据库查询结果集中名称一致的字段,mybatis会自动封装

  • 实体类属性名 和 数据库查询结果集中名称不一致的字段,不能自动封装,需要我们手动处理

 驼峰映射
 起别名

 

4.3 条件查询

 当查询方法出现多个查询条件时,需要使用@Param注解设置对应关系

    //#{} 里面的内容
    //1. 如果参数是一个对象类型, #{}写的是对象中的属性名
    //2. 如果参数是一个简单类型(8基本 + 8包装 +String) ,#{}可以随便写, 但是推荐写方法形参名
    //3. 如果参数是多个简单类型的参数, #{}中写的是@Param注解中的值
    //条件查询
    @Select("select * from emp where name = #{name} and gender = #{gender} and entrydate between #{begin} and #{end}")
    List<Emp> findList(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin, @Param("end") LocalDate end);

 4.4 模糊查询

5. 动态SQL

先来看一个需求:根据用户输入的内容查询员工列表(注意:每个条件都不是必填的),页面如下图

那么此时再用原来学习的知识,编写出如下的sql,还行不行呢?

select * from user where name= #{name} and gender = #{gender} and entrydate between #{begin} and #{end}

显然是有问题的,因为按照上面的sql来执行,如果用户只输入了姓名张三,得到的sql就变成了

select * from user where name= '张三' and gender = null and entrydate between null and null

很显然不符合我们的预期,我们预期是仅仅用户输入的查询条件出现在sql中,没输入的就不出现,即下面这样

select * from user where name= '张三'

像上面这样,==需要根据不同的条件来执行不同的sql语句的情况,就需要用到动态sql==。

在mybatis中,动态SQL是通过下面几个标签来实现的:

  • if 用于条件判断

  • where,set 用于格式控制

  • foreach 用于循环遍历

5.1 if和where

<!--字符串需要跟null和空串比较其他类型只要跟null比较if:使用test进行条件判断,只有条件成立,条件中的sql才会生效where:只会在<where>标签内部有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR。
-->
<select id="findByCondition" resultType="com.itheima.domain.Emp">select * from emp<where><if test="name != null and name != ''">name like concat('%',#{name},'%')</if><if test="gender != null">and gender = #{gender}</if><if test="begin != null and end != null">and entrydate between #{begin} and #{end}</if></where>
</select>
    //条件查询@Testpublic void testFindByCondition() {SqlSession sqlSession = MybatisUtil.getSqlSession();EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);//List<Emp> empList = empMapper.findByCondition("张", (short) 1, LocalDate.of(2002, 01, 01), LocalDate.of(2023, 12, 31));//List<Emp> empList = empMapper.findByCondition("张", (short) 1, null, null);List<Emp> empList = empMapper.findByCondition("", (short) 1, null, null);empList.forEach(e -> System.out.println(e));//lambda方式打印MybatisUtil.close(sqlSession);}

 5.2 if和Set

<update id="update">update emp<set><if test="username != null and username != ''">username = #{username},</if><if test="password != null and password != ''">password = #{password},</if><if test="name != null and name != ''">name = #{name},</if><if test="gender != null">gender = #{gender},</if><if test="image != null and image != ''">image = #{image},</if><if test="job != null">job = #{job},</if><if test="ed != null">entrydate = #{ed},</if><if test="deptId != null">dept_id = #{deptId},</if><if test="createTime != null">create_time = #{createTime},</if><if test="updateTime != null">update_time = #{updateTime},</if></set>where id = #{id}
</update>
//更新
@Test
public void testUpdate() {SqlSession sqlSession = MybatisUtil.getSqlSession();EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);Emp emp = new Emp();emp.setId(1);emp.setUsername("haha");empMapper.update(emp);MybatisUtil.close(sqlSession);
}

5.3 where

 

 

 

可见不一定是ID,item只要对得上就能删除 

关键字:软文营销平台_微信运营模式_小网站怎么搜关键词_肇庆疫情最新消息

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: