Mybatis入门篇
Mybatis
简介:
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
中文文档: https://mybatis.org/mybatis-3/zh/index.html
下载地址: https://github.com/mybatis/mybatis-3/releases
maven仓库:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency>
1.2持久层,持久化
数据持久化将程序
2.1环境搭建
新建一个普通的maven项目
删除src目录
导入maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--父工程--> <groupId>org.example</groupId> <artifactId>Mybatis</artifactId> <version>1.0-SNAPSHOT</version> <!--导入依赖--> <dependencies> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--每一个Mapper。xml都需要在Mybatis核心配置文件中注册--> </dependencies> </project>
4.建一个maven项目
5.在resources文件下新建一个xml文件,可通过右侧连接
2.2配置文件
<?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> <!--环境--> <environments default="development"> <environment id="development"> <!--事务管理默认JDBC--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--四个属性--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> ? </configuration>
2.3编写工具类utils
package com.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; //sqlSessionFactory-->sqlSession public class MybatisUtils { private static /*它用来增删改查*/SqlSessionFactory sqlSessionFactory; static { try {/*使用MybatisUtils第一步获取SQL sessionFactory对象*/ String resource = "/*要被反射的名称*/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace();}} //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。 // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。 // 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。 public static SqlSession getsSqlSession(){ return sqlSessionFactory.openSession(); } }
2.3编写代码
实体类
package com.pojo; ? public class User{ private int id; private String name; private String pwd; ? public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", pwd=‘" + pwd + ‘\‘‘ + ‘}‘; } 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; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; }}
Dao接口
public interface Userdao { List<User> getUserList();}
接口实现类
<?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 绑定一个对应的Dao/Mapper接口--> <mapper namespace="com.dao.Userdao"> <!--select查询语句 ,其中id对应Userdao中的方法的名字, resultType对应着返回的类型,resultMap对应着返回的集合,要写全路径--> <select id="getUserList" resultType="com.pojo.User"> select * from mybaits.user; </select> </mapper>
2.4测试
注意点:每一个Mapper.xml都需要在Mybatis核心配置文件中注册
<mapper resource="com/dao/UserMapper.xml"/></mappers>
在build中配置resource,来防止我们资源到处失败的问题
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.propertise</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.prppertis</include> <include>**/*.xml</include> </includes> </resource> </resources></build>
###mapper.xml 配置文件有中文可能报错。
###<!-- <filtering>false</filtering>-->否则可能报错
###useSSL=false否则也可能报错
相关推荐
liqiancao 2020-07-26
chenjiazhu 2020-07-08
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25