概述
1.导入相关依赖:pom.xml导入 Mybatis依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
</dependencies>
2.配置XML 配置文件中包含了对 MyBatis 系统的核心设置
在resources中创建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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/aaa?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
注意:修改url,username,password
3.编写util
package com.tao.util;
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 {
String resource = "mybatis-config.xml";
InputStream inputStream = inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//有了sqlSessionFactory,就可以获得sqlsession的实例了
//sqlSession 完全包含了面向数据库执行sql命令所需的所有方法
public static SqlSession getSqlsession(){
return sqlSessionFactory.openSession();
}
}
4.写实体类
package com.tao.info;
public class userinfo {
private int id;
private String name;
public userinfo() {
}
public userinfo(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "userInfo{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
}
5.写接口
package com.tao.dao;
import com.tao.info.userinfo;
import java.util.List;
public interface UserDao {
List<userinfo> getUserList();
}
6.在resources下配置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.tao.dao.UserDao">
<select id="getUserList" resultType="com.tao.info.userinfo">
select * from user
</select>
</mapper>
namesepace:为接口路径
id:接口中的方法名
resultType:为实体类路径(sql语句返回值)
7.测试
package org.tao.dao;
import com.tao.dao.UserDao;
import com.tao.info.userinfo;
import com.tao.util.mybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
//获取SqlSession对象
SqlSession sqlsession = mybatisUtils.getSqlsession();
//getMapper
UserDao mapper = sqlsession.getMapper(UserDao.class);
List<userinfo> userList = mapper.getUserList();
for (userinfo userinfo : userList) {
System.out.println(userinfo);
}
//关闭sqlSession
sqlsession.close();
}
}
运行之后报错
资源导出问题,找不到
pom.xml中导入如下代码:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
8.运行
最后
以上就是热情指甲油为你收集整理的Idea2021.3.1 新建第一个Mybatis程序,Mybatis入门的全部内容,希望文章能够帮你解决Idea2021.3.1 新建第一个Mybatis程序,Mybatis入门所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复