JAVA MyBatis入门学习过程记录
作者:wangweizu99 时间:2022-04-23 13:49:24
一、Mybatis
1、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>
<!-- 外部引入db-->
<properties resource="db.properties"/>
<settings>
<!-- 配置日志文件-->
<!-- <setting name="logImpl" value="STDOUT_LOGGING"/>-->
<setting name="logImpl" value="log4j"/>
<!-- 开启A_NAME==>aName-->
<setting name="mapUnderscoreToCamelCase" valie="true"
</settings>
<!-- 减少类型-->
<typeAliases> <typeAlias type="com.wang.pojo.User" alias="User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/wang/dao/UserMapper.xml"/>
</mappers>
</configuration>
2、Mapper.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">
<mapper namespace="com.wang.dao.UserDao">
<select id="getUserList" resultType="User">
select * from mybatis.user
</select>
</mapper>
3、db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=123456
4、MybatisUtils工具类
package com.wang.utils;
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.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
InputStream inputStream = null;
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getsqlsession() {
return sqlSessionFactory.openSession();
}
}
5、多对一AND多对一:
在Mapper.xml中配置
association 对象 多对一
collection 集合 一对多
从用参数解释:
javaType java类型
ofType 泛型约束类型
6、使用注解开发
可在接口中使用注解进行开发,省去Mapper.xml
@Select("select * from mybatis.User")
List<User> getUserList();
在mybatis-config.xml中配置
<mappers>
<mapper class="com.wang.Dao.UserMapper"
</mappers>
7、自定义缓存:ehcache
8、mybatis 其他工具
mybatis Hepler:https://pagehelper.github.io/
二、MAVEN资源导出错误解决
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
三、常用依赖导入
<!--导入依赖-->
<dependencies>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- servlet and jsp-->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
<version>6.1.7</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<!-- standard-->
<dependency>
<groupId>org.apache.karaf.assemblies.features</groupId>
<artifactId>standard</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- jstl-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!--Log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
四、LOG4J
1、log4j.properties 配置文件
<!--导入依赖-->
<dependencies>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- servlet and jsp-->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
<version>6.1.7</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<!-- standard-->
<dependency>
<groupId>org.apache.karaf.assemblies.features</groupId>
<artifactId>standard</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- jstl-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!--Log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
2、log4j 在mybatis-config.xml的配置
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
3、使用场景
在具体类中引入
Logger logger = Logger.getLogger(UserDaoTest.class);
logger.error();
logger.debug();
好用的插件:
1、lombok
常用注解:
@Data 自动生成geter seter
@AllArgsConstructor 生成有参构造函数
@NoArgsConstructor 生成无参构造函数
=“logImpl” value=“LOG4J”/>
## 3、使用场景
- 在具体类中引入
```java
Logger logger = Logger.getLogger(UserDaoTest.class);
logger.error();
logger.debug();
常用注解:
@Data 自动生成geter seter
@AllArgsConstructor 生成有参构造函数
@NoArgsConstructor 生成无参构造函数
来源:https://blog.csdn.net/weixin_50738670/article/details/117933388
标签:JAVA,MyBatis,入门
0
投稿
猜你喜欢
idea创建javaweb原生项目的实现示例
2023-06-16 05:40:35
c#代码生成URL地址的示例
2022-02-17 09:44:08
Java 关于时间复杂度和空间复杂度的深度刨析
2023-11-10 16:07:39
Android 如何从零开始写一款书籍阅读器的示例
2022-10-20 17:52:08
Spring Boot使用AOP实现REST接口简易灵活的安全认证的方法
2023-04-10 04:38:44
Java模拟QQ桌面截图功能实现方法
2021-09-19 16:30:02
springboot整合JSR303校验功能实现代码
2023-10-11 21:46:25
Android7.0版本影响开发的改进分析
2022-06-06 03:40:37
Java方法重载Overload原理及使用解析
2021-11-21 14:23:14
Idea中maven项目实现登录验证码功能
2023-11-29 16:50:51
Java使用Random类生成随机数示例
2022-08-19 04:22:19
详解Java实现数据结构之并查集
2023-09-05 08:47:06
Android仿支付宝手势密码解锁功能
2023-02-14 09:06:46
WPF实现时钟特效
2021-11-13 11:16:24
java泛型中占位符T和?有什么区别
2022-07-05 02:53:56
为Android应用增加渠道信息 自动化不同渠道的打包过程的使用详解
2022-06-01 11:38:22
Android之在linux终端执行shell脚本直接打印当前运行app的日志的实现方法
2021-06-12 23:41:08
Android中判断网络是否可用的代码分享
2022-01-03 23:59:46
C#一个方法返回多个值示例
2023-06-22 18:38:49
Android自定义View实现开关按钮
2021-09-08 08:53:22