什么是Mybatis
这是引用mybatis官网的一个解释什么是mybatis,简短点说就是帮助我们替换掉原有的jdbc操作数据库,然后对数据类型做出相应的转换,参数和结果集的封装。
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
如何使用jdbc怎么连接数据库
1. 加载驱动
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
2. 获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false", "xxx", "xxx");
3. 获取statement对象
//获取statement对象
statement = connection.createStatement();
4. 使用statement对象执行sql
resultSet = statement.executeQuery("select * from user where id=2");
5. 对返回对象ResultSet结果集做数据处理
//遍历ResultSet结果集
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
queryString+=id+name;
}
6.关闭Connection,Statement,ResultSet三个对象
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
JDBC连接总的代码处理如下:
public class TestJDBCConnection {
public static void main(String[] args) {
/**
* 使用jdbc连接数据库的步骤:
* 加载驱动
* 获取连接
* 创建statement对象
* 执行sql
* 关闭sql,执行声明
* 关闭连接
*/
Connection connection = null;
Statement statement = null;
int count = 0;
ResultSet resultSet = null;
String queryString = "";
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false", "xxx", "xxx");
//创建statement对象
statement = connection.createStatement();
//INSERT , DELETE , UPDATE ,增删改使用,statement接口中的,executeUpdate(String sql)返回值 int.
//执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。
//int insert_into_ = statement.executeUpdate("insert into ");
//Query查询 executeQuery(String sql)返回值为ResultSet
resultSet = statement.executeQuery("select * from user where id=2");
//遍历ResultSet结果集
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
queryString+=id+name;
}
System.out.println(queryString);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
使用JDBC操作数据的结果:
如何使用mybatis的XML配置怎么代替JDBC连接数据库
1.通过XML配置文件获取对应的InputStream流
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
2.通过SqlSessionFactoryBuild新建一个SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
3.通过SqlSessionFactory新建一个SqlSession对象,通过SqlSession对象操作数据
try(SqlSession sqlSession = sqlSessionFactory.openSession()){
MybatisMapper mapper = sqlSession.getMapper(MybatisMapper.class);
str = mapper.selectById();
}
4.项目结构如下:
5. mybatis.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatismapper.xml"/>
</mappers>
</configuration>
6. mybatismapper.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="org.apache.ibatis.mybatistestcustom.MybatisMapper">
<select id="selectById" resultType="java.lang.String">
select name from user where id= 2
</select>
</mapper>
7. mybatismapper.xml对应的接口MybatisMapper接口内容如下:
public interface MybatisMapper {
String selectById();
}
Mybatis的XML配置总的代码逻辑如下:
public class TestMybatisConnection {
public static void main(String[] args) throws IOException {
//1.先获得的sqlsessionFactory,这个可以通过sqlsessionfactorybuilder获取,sqlsessionFactory可以有xml,java配置两种形式来排至
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 通过sqlsessionFactory或的sqlsession,sqlsession里面有处理CURD的所有方法
//这里有两种形式,可以通过直接执行语句,也可以调用接口的方法执行
//接口方法
String str = "";
try(SqlSession sqlSession = sqlSessionFactory.openSession()){
MybatisMapper mapper = sqlSession.getMapper(MybatisMapper.class);
str = mapper.selectById();
}
System.out.println(str);
//直接执行语句
// try(SqlSession sqlSession = sqlSessionFactory.openSession()){
// String str = sqlSession.selectOne("com.yzg.demo.MybatisMapper.selectById",null);
// }
}
}