技术标签: spring java # Spring framework mybatis mysql 数据库
目录
如果您对Spring基础框架不是很了解请先看Spring 配置详解。
1、MyBatis(前身为iBatis)是一个开源的Java持久层框架,它主要用于与数据库交互,帮助开发者更轻松地进行数据库操作。
2、MyBatis 提供了一种简单而强大的方式来执行数据库操作,包括查询、插入、更新和删除。开发者可以使用XML配置文件或者注解来定义SQL语句,并且可以将SQL语句的参数映射到Java对象,以便进行数据库操作。
3、MyBatis 提供了缓存机制,可以帮助提高查询性能。你可以配置缓存来存储查询结果,以便在后续查询中重用结果,从而减少数据库访问次数。
4、MyBatis 支持事务管理,可以确保数据库操作的原子性和一致性。你可以通过配置或编程方式管理事务,以满足应用程序的需求。
1、创建数据库和表
2、添加maven依赖
3、编写实体类
4、创建XXXMapper接口
5、配置XXXMapper.xml
6、配置数据库连接信息 JDBC
7、在Spring中配置Mybatis
8、在Spring中调用Mybatis查询数据
项目整体结构如下图:
请自行安装数据库并创建表。我的数据库名为yiqifu,表为名u_user,字段如下图。
CREATE TABLE `U_USER` (
`id` int(255) NULL ,
`nickname` varchar(255) NULL
)
;
分别添加mysql-connector-java(用于连接mysql数据库)、mybatis(mybatis核心库)、mybatis-spring(在spring中使用mybatis的库)的maven依赖包。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.29</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!--mybatis-spring包是适配包,帮助你在Spring中使用MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--可以使用阿里巴巴的DruidDataSource-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.29</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
<scope>test</scope>
</dependency>
根据数据表的字段创建一个实体类,各字段可以不跟数据库一致。对应关系可以在Mapper.xml中配置
package top.yiqifu.study.p061_mybatis;
public class UserEntity {
public int id;
public String name;
public UserEntity(){
}
public UserEntity(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
UserMapper接口的作用是告诉mybatis您要对数据库执行那些操作。具体实现类由Spring AOP完成。其中执行的SQL语句可以通过Mappser.xml配置,也可以在这里使用注解配置。我这里仅定义了几个简单的增删改查。
package top.yiqifu.study.p061_mybatis;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
void insert(UserEntity user);
void deleteById(Integer id);
void update(UserEntity user);
List<UserEntity> findAll();
@Select("select * from u_user where id=#{id}")
@Results({
@Result(property = "name", column = "nickname"), // 指定属性名和列名的映射关系
})
UserEntity findById(Integer id);
}
UserMapper.xml的作用是告诉mybatis您在UserMapper接口定义的方法具体使用什么样的SQL及其他约束。我这里简单配置了UserMapper接口中每个方法。
注:其实也可以直接在UserMapper接口中使用注解定义(请看findById方法),使用XML定义是为了解耦。
<?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">
<!-- namespace 对应到 接口上 就不需要实例化了 -->
<mapper namespace="top.yiqifu.study.p061_mybatis.UserMapper">
<resultMap type="top.yiqifu.study.p061_mybatis.UserEntity" id="BaseResultMap">
<!-- column 表里的字段 -->
<!-- property 实体对象里的属性 -->
<result column="id" property="id"/>
<result column="nickname" property="name"/>
</resultMap>
<insert id="insert" parameterType="top.yiqifu.study.p061_mybatis.UserEntity">
insert into u_user(nickname)
values
(
#{name}
)
</insert>
<insert id="deleteById" parameterType="Integer">
delete from u_user where id=#{id}
</insert>
<insert id="update" parameterType="top.yiqifu.study.p061_mybatis.UserEntity">
update u_user set nickname=#{name} where id=#{id}
</insert>
<select id="findAll" resultMap="BaseResultMap">
select * from u_user
</select>
</mapper>
配置jdbc.properties(放在resources目录下)的作用时指定数据连接信息。
database.driver=com.mysql.cj.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/yiqifu?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
database.username=root
database.password=123456xxoo
配置applicationContext-mybatis.xml(放在resources目录下)的作用将前的内容整合在起。包括mybatis的JDBC环境,接口映射和接口配置。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:component-scan base-package="top.yiqifu.study.p061_mybatis"></context:component-scan>
<!-- 加载jdbc.properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 MyBatis 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<!-- 配置 mybatis Session -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 扫描Mapper接口(Spring会使用AOP为其实现具体方法) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.yiqifu.study.p061_mybatis" />
</bean>
</beans>
以下是调用示例。
package top.yiqifu.study.p061_mybatis;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.yiqifu.study.p051_proxy.Test011_StaticProxyDog;
import top.yiqifu.study.p051_proxy.Test041_Animal;
import top.yiqifu.study.p051_proxy.Test042_Dog;
public class Test001_Mybatis
{
// 静态代理
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
UserMapper mapper = context.getBean("userMapper", UserMapper.class);
for(String beanName : context.getBeanDefinitionNames()){
System.out.println(beanName);
}
UserEntity user1 = new UserEntity();
UserEntity user2 ;
//添加
user1.setName("test");
mapper.insert(user1);
int userId = 7;
user2 = mapper.findById(userId);
System.out.println(user2);
//修改
user1.setId(userId);
user1.setName("new-test");
mapper.update(user1);
//查询
user2 = mapper.findById(userId);
System.out.println(user2);
//删除
//mapper.deleteById(userId);
user2 = mapper.findById(userId);
System.out.println(user2);
}
}
文章浏览阅读5.8k次。在大数据的发展当中,大数据技术生态的组件,也在不断地拓展开来,而其中的Hive组件,作为Hadoop的数据仓库工具,可以实现对Hadoop集群当中的大规模数据进行相应的数据处理。今天我们的大数据入门分享,就主要来讲讲,Hive应用场景。关于Hive,首先需要明确的一点就是,Hive并非数据库,Hive所提供的数据存储、查询和分析功能,本质上来说,并非传统数据库所提供的存储、查询、分析功能。Hive..._hive应用场景
文章浏览阅读496次。Zblog是由Zblog开发团队开发的一款小巧而强大的基于Asp和PHP平台的开源程序,但是插件市场上的Zblog采集插件,没有一款能打的,要么就是没有SEO文章内容处理,要么就是功能单一。很少有适合SEO站长的Zblog采集。人们都知道Zblog采集接口都是对Zblog采集不熟悉的人做的,很多人采取模拟登陆的方法进行发布文章,也有很多人直接操作数据库发布文章,然而这些都或多或少的产生各种问题,发布速度慢、文章内容未经严格过滤,导致安全性问题、不能发Tag、不能自动创建分类等。但是使用Zblog采._zblog 网页采集插件
文章浏览阅读2.4k次,点赞2次,收藏2次。restUI页面提交1.1 添加上传jar包1.2 提交任务job1.3 查看提交的任务2. 命令行提交./flink-1.9.3/bin/flink run -c com.qu.wc.StreamWordCount -p 2 FlinkTutorial-1.0-SNAPSHOT.jar3. 命令行查看正在运行的job./flink-1.9.3/bin/flink list4. 命令行查看所有job./flink-1.9.3/bin/flink list --all._flink定时运行job
文章浏览阅读1k次,点赞2次,收藏6次。这个项目是基于STM32的LED闪烁项目,主要目的是让学习者熟悉STM32的基本操作和编程方法。在这个项目中,我们将使用STM32作为控制器,通过对GPIO口的控制实现LED灯的闪烁。这个STM32 LED闪烁的项目是一个非常简单的入门项目,但它可以帮助学习者熟悉STM32的编程方法和GPIO口的使用。在这个项目中,我们通过对GPIO口的控制实现了LED灯的闪烁。LED闪烁是STM32入门课程的基础操作之一,它旨在教学生如何使用STM32开发板控制LED灯的闪烁。_嵌入式stm32闪烁led实验总结
文章浏览阅读63次。本文介绍了安装和部署Debezium的详细步骤,并演示了如何将Debezium服务托管到systemctl以进行方便的管理。本文将详细介绍如何安装和部署Debezium,并将其服务托管到systemctl。解压缩后,将得到一个名为"debezium"的目录,其中包含Debezium的二进制文件和其他必要的资源。注意替换"ExecStart"中的"/path/to/debezium"为实际的Debezium目录路径。接下来,需要下载Debezium的压缩包,并将其解压到所需的目录。
文章浏览阅读4.4k次。需求:在诗词曲文项目中,诗词整篇朗读的时候,文章没有读完会因为屏幕熄灭停止朗读。要求:在文章没有朗读完毕之前屏幕常亮,读完以后屏幕常亮关闭;1.权限配置:设置电源管理的权限。
文章浏览阅读2.3k次。目标检测简介、评估标准、经典算法_目标检测
文章浏览阅读6.3k次,点赞4次,收藏9次。实训时需要安装SQL server2008 R所以我上网上找了一个.exe 的安装包链接:https://pan.baidu.com/s/1_FkhB8XJy3Js_rFADhdtmA提取码:ztki注:解压后1.04G安装时Microsoft需下载.NET,更新安装后会自动安装如下:点击第一个傻瓜式安装,唯一注意的是在修改路径的时候如下不可修改:到安装实例的时候就可以修改啦数据..._sqlserver 127 0 01 无法连接
文章浏览阅读7.4k次。1. Object.keys(item); 获取到了key之后就可以遍历的时候直接使用这个进行遍历所有的key跟valuevar infoItem={ name:'xiaowu', age:'18',}//的出来的keys就是[name,age]var keys=Object.keys(infoItem);2. 通常用于以下实力中 <div *ngFor="let item of keys"> <div>{{item}}.._js 遍历对象的key
文章浏览阅读2.2w次,点赞51次,收藏310次。粒子群算法求解路径规划路径规划问题描述 给定环境信息,如果该环境内有障碍物,寻求起始点到目标点的最短路径, 并且路径不能与障碍物相交,如图 1.1.1 所示。1.2 粒子群算法求解1.2.1 求解思路 粒子群优化算法(PSO),粒子群中的每一个粒子都代表一个问题的可能解, 通过粒子个体的简单行为,群体内的信息交互实现问题求解的智能性。 在路径规划中,我们将每一条路径规划为一个粒子,每个粒子群群有 n 个粒 子,即有 n 条路径,同时,每个粒子又有 m 个染色体,即中间过渡点的_粒子群算法路径规划
文章浏览阅读353次。所谓稳健的评估指标,是指在评估的过程中数据的轻微变化并不会显著的影响一个统计指标。而不稳健的评估指标则相反,在对交易系统进行回测时,参数值的轻微变化会带来不稳健指标的大幅变化。对于不稳健的评估指标,任何对数据有影响的因素都会对测试结果产生过大的影响,这很容易导致数据过拟合。_rar 海龟
文章浏览阅读607次,点赞2次,收藏7次。–基于STM32F103ZET6的UART通讯实现一、什么是IAP,为什么要IAPIAP即为In Application Programming(在应用中编程),一般情况下,以STM32F10x系列芯片为主控制器的设备在出厂时就已经使用J-Link仿真器将应用代码烧录了,如果在设备使用过程中需要进行应用代码的更换、升级等操作的话,则可能需要将设备返回原厂并拆解出来再使用J-Link重新烧录代码,这就增加了很多不必要的麻烦。站在用户的角度来说,就是能让用户自己来更换设备里边的代码程序而厂家这边只需要提供给_value line devices connectivity line devices