流程
- 编写核心配置文件
- 编写·sql映射配置文件
- 创建模型类
- 编写主程序代码
核心配置文件
<?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://10.30.59.136/wy?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--sql映射文件路径-->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
sql映射配置文件
<?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="test">
<!--查询语句并设置唯一ID号-->
<select id="selectzb" resultType="com.wy.demo.User">
select * from zhibo
</select>
</mapper>
模型类
package com.wy.demo;/*
coding: utf-8
Created: by WAY on 2021/11/29 16:01
*/
public class User {
private int id;
private String game_name;
private String name;
private String title;
private String hot;
private String pt;
@Override
public String toString() {
return "User{" +
"id=" + id +
", game_name='" + game_name + '\'' +
", name='" + name + '\'' +
", title='" + title + '\'' +
", hot='" + hot + '\'' +
", pt='" + pt + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGame_name() {
return game_name;
}
public void setGame_name(String game_name) {
this.game_name = game_name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getHot() {
return hot;
}
public void setHot(String hot) {
this.hot = hot;
}
public String getPt() {
return pt;
}
public void setPt(String pt) {
this.pt = pt;
}
}
主程序编写
package com.wy.test;
/*
coding: utf-8
Created: by WAY on 2021/11/29 16:42
*/
import com.wy.demo.User;
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;
import java.util.List;
public class test {
public static void main(String[] args) throws IOException {
//设置核心配置文件路径
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession对象,用来执行sql
SqlSession sqlsession = sqlSessionFactory.openSession();
//执行sql
List<User> users = sqlsession.selectList("test.selectzb");
for(int i=0;i<users.size();i++){
System.out.println(users.get(i));
}
//释放资源
sqlsession.close();
}
}
Mapper接口代理
- 定义与SQL映射文件同名的Mapper接口,将接口和SQL映射文件放在同一目录下
- 设置SQL映射文件的名称空间为Mapper接口全限定名
- 在Mapper接口中定义方法,方法名为SQL映射文件中sql语句的ID,返回值类型为List<模型类>
- 调用SqlSession中的getMapper方法获取代理对象执行sql语句
SQL映射文件
<?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接口全限定名-->
<mapper namespace="com.wy.Mapper.UserMapper">
<!--查询语句并设置唯一ID号-->
<select id="selectzb" resultType="com.wy.demo.User">
select * from zhibo
</select>
</mapper>
接口
```java package com.wy.Mapper;
import com.wy.demo.User;
import java.util.List;
public interface UserMapper {
List
<a name="bnvXA"></a>
## 执行sql语句
```java
package com.wy.test;
/*
coding: utf-8
Created: by WAY on 2021/11/29 16:42
*/
import com.wy.Mapper.UserMapper;
import com.wy.demo.User;
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;
import java.util.List;
public class test {
public static void main(String[] args) throws IOException {
String resource = "com/wy/Mapper/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession对象,用来执行sql
SqlSession sqlsession = sqlSessionFactory.openSession();
//执行sql
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<User> selectzb = mapper.selectzb();
for (User i:selectzb){
System.out.println(i);
}
//释放资源
sqlsession.close();
}
}