MyBatis1:初识

MyBatis第一个程序

流程:搭建环境–》导入MyBatis–》编写代码–》测试

1、创建一张User表。
关键字id、username、pwd

2、导入相关jar包

    <dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>

3、编写MyBatis核心配置文件

<?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>
<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?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="Su190419.."/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

4、编写工具类

public class MyBatiUtil {
private static SqlSessionFactory sqlSessionFactory;
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
} static{
String resource = "mybatis-config.xml";
try {
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
} }

5、实体类

package com.pp.pojo;
@Data
public class User {
private int id;
private String name;
private String pwd;
}

6、编写Mapper(DAO)接口类

public interface UserDao {

    List<User> getUserLike(String value);
List<User> getUserList();
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id); }

7、编写Mapper.xml配置文件
namespace十分重要,不能写错

<?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.pp.dao.UserDao">
<select id="getUserLike" resultType="com.pp.pojo.User">
select * from mybatis.user where name like #{value}
</select>
</mapper>

8、编写测试类

  • Junit包测试
public class MyTest {
@Test
public void selectUser() {
SqlSession session = MybatisUtils.getSession();
//方法一:
//List<User> users = session.selectList("com.kuang.mapper.UserMapper.selectUser");
//方法二:
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.selectUser(); for (User user: users){
System.out.println(user);
}
session.close();
}
}

9、db.properties

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
username = root
password = Su190419..

10、log4j.properties

#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger = DEBUG,console ,file #控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%c]-%m%n #文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File = ./log/pp.log
log4j.appender.file.MaxFileSize = 10mb
log4j.appender.file.Threshold = DEBUG
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = [%p][%d{yy-MM-dd}][%c]%m%n #日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

MyBatis——第一个程序的更多相关文章

  1. mybatis第一个程序随笔

    今天继续学习了解如何写一个mybatis程序 创建了Dao层 1.1 创建一个UserDao接口 1.2 创建UserMapper.xml文件 在mybaits中文手册查找配置信息 <?xml ...

  2. MyBatis第一个程序

    创建一个maven项目,并且在pom.xml导入myBatis和jdbc的jar包 <dependencies> <dependency> <groupId>org ...

  3. JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明

    1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...

  4. Mybatis第一天(其他)

    Mybatis第一天 框架课程 课程计划 第一天: Mybatis的介绍 Mybatis的入门 使用jdbc操作数据库存在的问题 Mybatis的架构 Mybatis的入门程序 Dao的开发方法 原始 ...

  5. Mybatis第一天

    Mybatis第一天   框架课程 1.   课程计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a)       使用jdbc操作数据库存在的问题 b)      Mybatis ...

  6. mybatis第一天——入门与概述

    大纲摘要: 1.mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 a) 原始da ...

  7. Spring的第一个程序

    目录 一.Spring概述 1. Spring是什么? 2. IOC控制反转 二.Spring的第一个程序 1. 创建Maven项目 2. 加入maven依赖pom.xml 3. 定义接口和实体类 4 ...

  8. Android开发-之第一个程序:HelloWorld!

    小编觉得不管学习什么编程的时候,第一个程序都是要求打印输出一个"HelloWorld!",那就从最简单的HelloWorld开始吧!哈哈~~~~ 一.创建一个Android工程 1 ...

  9. [Fluent NHibernate]第一个程序

    目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...

  10. rails再体验(第一个程序)

    掌握redmine plugin开发的目标在2016年未实现,2017年继续. 选择<Ruby on Rails Tutorial>教程,windows安装railsinstaller,该 ...

随机推荐

  1. 专业的字节码查看工具——jclasslib

    下载地址: GitHub地址:点击下方蓝色链接即可. https://github.com/ingokegel/jclasslib/releases/download/6.0.4/jclasslib_ ...

  2. ACTF flutter逆向学习

    参考了许多大佬的博客,在此特别诚挚感谢oacia大佬和其他大佬的博客和指导! 1.flutter和apk基础结构介绍 首先下载附件,是一个apk文件,用jadx打开 可以看见flutter字样,而fl ...

  3. 【GIT-精讲】从零玩转Git-基础理论

    关于版本控制 一.什么是版本控制 版本控制(Version Control Systems)版本控制(Revision control)是一种软件工程技巧 在开发的过程中,确保由不同人所编辑的同一档案 ...

  4. 2024-01-06:用go语言,在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧 在桥上有一些石子,青蛙很讨厌踩在这些石子上 由于桥的长度和青蛙一次跳过的距离都是正整数 我们可以把独木桥

    2024-01-06:用go语言,在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧 在桥上有一些石子,青蛙很讨厌踩在这些石子上 由于桥的长度和青蛙一次跳过的距离都是正整数 我们可以把独木桥 ...

  5. electron入门之通知Notification(二)

    electron入门到入土,从渲染线程中创建新窗口.2022-03-21入门版本17.1.2 electron重要概念,只有一个主线程,其他都是渲染进程或者叫子线程,他们不能直接相互操作,可以通过ip ...

  6. ORA-28579 在从外部过程代理程序回调时,发生网络错误,ORA-06512 在"SDE.ST_GEOMETRY_SHAPELIB_PKG"

    如图所示,在执行sde.st_transform方法时报错. 环境是arcgis10.8  oracle是12C,版本号是v12.1.0.2.0 但是执行ST_GEOMETRY方法是可以的,说明配置没 ...

  7. Istio与Kubernetes:资源管理与协同解析

    本文分享自华为云社区<istio资源介绍以及和kubernetes资源扭转关系>,作者:可以交个朋友. 一.istio原理 Istio的原理是拦截 Kubernetes 中创建 Pod 的 ...

  8. 如何极速极速搭建个人博客?Copy攻城狮用的这一招很优秀!

    摘要:在中国功夫中,"天下武功,无坚不摧,唯快不破",在编程的世界里,如何快速搭建一个属于自己的博客呢?那么 Pagic + Vercel 应该是个不错的选择!接下来,由Copy攻 ...

  9. 提升80%上云集成效率, TA是如何做到的

    摘要:基于华为云开天aPaaS,提升80%上云集成效率,降低50%集成成本 没有充足资金,没有足够的项目规划和过渡时间,也没有经验丰富的IT团队支持,中小企业的上云路可谓是困难重重.如何帮助企业高效上 ...

  10. git clone 出现fatal: unable to access ‘https://github 错误解决方法

    git clone 遇到问题:fatal: unable to access 'https://github.comxxxxxxxxxxx': Failed to connect to xxxxxxx ...