mybatis基础项目,maven创建,工具idea
作者:互联网
这里写自定义目录标题
1.新建maven项目
使用模板后一路next即可。
创建好后项目结构是这样的:
补上资源文件:
2.代码部分
2.0:前期准备(数据库建表)
创建一个student表:
写入数据:
导入包:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
2.1:编写实体Student类
2.2:编写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>
<properties resource="dbconfig.properties"></properties>
<!-- <typeAliases>-->
<!-- <typeAlias type="com.zyq.pojo.Student" alias="Student"></typeAlias>-->
<!-- </typeAliases>-->
<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/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zyq/dao/StudentMapper.xml"/>
</mappers>
</configuration>
2.3:编写dao层的StudentDao
package com.zyq.dao;
import com.zyq.pojo.Student;
import java.util.List;
public interface StudentDao {
// 查询所有学生的接口
// 后面的增删改查自行增加,方便起见这里只写一个查询
public List<Student> selectStudents();
}
2.4:编写mapper层:StudentMapper.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对应一个接口
里面写数据库语句,写完后还要在mybatis-config.xml绑定mapper
-->
<mapper namespace="com.zyq.dao.StudentDao">
<select id="selectStudents" resultType="com.zyq.pojo.Student">
select*from student
</select>
</mapper>
2.5:编写Test测试类,查询数据
import com.zyq.dao.StudentDao;
import com.zyq.pojo.Student;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
@Test
public void selectStu() {
System.out.println("查询所有学生的请求正在处理");
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> student = studentDao.selectStudents();
for (Student student1 : student) {
System.out.println(student1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.6:测试成功与失败
测试成功:
出现bug:
出现bug的原因很可能是编译的时候,StudentMapper.xml没有注入到target中:
ps:这是成功注入的,如果只有StudentDao说明没有注入成功。
手动注入:
方法一:在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>
方法二:在mybatis-config.xml中手动注入:
<mappers>
<mapper resource="com/zyq/dao/StudentMapper.xml"/>
</mappers>
-----------------------------------------------------------------------------------------
到此结束,谢谢~
标签:xml,java,StudentDao,idea,maven,import,mybatis,org 来源: https://blog.csdn.net/m0_47306534/article/details/115260838