try{ String resources="mybatis-config.xml"; Reader reader=null; reader= Resources.getResourceAsReader(resources); sqlMapper=new SqlSessionFactoryBuilder().build(reader); }
时间: 2024-02-14 17:04:48 浏览: 139
这段代码是用于创建 MyBatis 的 SqlSessionFactory 实例,其中通过读取 MyBatis 的配置文件 mybatis-config.xml 来构建一个 Reader 对象,然后通过 SqlSessionFactoryBuilder 的 build 方法将 Reader 对象转换为 SqlSessionFactory 对象。SqlSessionFactory 是 MyBatis 的核心接口之一,它负责创建 SqlSession 对象,而 SqlSession 对象则用于执行 SQL 语句和管理事务等操作。这段代码是 MyBatis 中常用的初始化方式。
相关问题
基于MyBatis注解的学生管理程序
这里是一个简单的基于MyBatis注解的学生管理程序示例:
1. 创建数据库和表
首先,我们需要创建一个名为 "student" 的数据库,并在其中创建一个名为 "student_info" 的表。表中包含以下字段:id、name、age、gender、address。
2. 创建Java项目并导入MyBatis依赖
在Eclipse或者IntelliJ IDEA中创建一个Java项目,并在项目的pom.xml文件中加入以下依赖:
```
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.x.x</version>
</dependency>
```
3. 创建实体类
创建一个名为 "Student" 的实体类,包含以下属性:id、name、age、gender、address,并提供相应的setter和getter方法。
```
public class Student {
private int id;
private String name;
private int age;
private boolean gender;
private String address;
// setter和getter方法
}
```
4. 创建MyBatis映射文件
在src/main/resources目录下创建一个名为"StudentMapper.xml"的文件,并编写相应的MyBatis映射语句。这里使用注解的方式来实现映射,示例代码如下:
```
public interface StudentMapper {
@Select("SELECT * FROM student_info WHERE id = #{id}")
public Student getStudentById(int id);
@Insert("INSERT INTO student_info(name, age, gender, address) VALUES(#{name}, #{age}, #{gender}, #{address})")
public int addStudent(Student student);
@Delete("DELETE FROM student_info WHERE id = #{id}")
public int deleteStudent(int id);
@Update("UPDATE student_info SET name = #{name}, age = #{age}, gender = #{gender}, address = #{address} WHERE id = #{id}")
public int updateStudent(Student student);
}
```
5. 创建MyBatis配置文件
在src/main/resources目录下创建一个名为 "mybatis-config.xml" 的文件,并配置MyBatis的相关参数,示例代码如下:
```
<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/student"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
```
6. 创建MyBatis工厂
创建一个名为 "MyBatisUtil" 的工具类,用于创建MyBatis的SqlSessionFactory对象,示例代码如下:
```
public class MyBatisUtil {
private static SqlSessionFactory sessionFactory = null;
static {
try {
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sessionFactory;
}
}
```
7. 创建测试类
在src/test/java目录下创建一个名为 "StudentTest" 的测试类,用于测试操作数据库的功能。
```
public class StudentTest {
@Test
public void testGetStudentById() {
SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.getStudentById(1);
System.out.println(student.getName() + " " + student.getAge() + " " + student.getGender() + " " + student.getAddress());
} finally {
session.close();
}
}
@Test
public void testAddStudent() {
SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setName("小明");
student.setAge(18);
student.setGender(true);
student.setAddress("北京市朝阳区");
int count = mapper.addStudent(student);
session.commit();
System.out.println("添加了" + count + "条记录");
} finally {
session.close();
}
}
@Test
public void testDeleteStudent() {
SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
int count = mapper.deleteStudent(1);
session.commit();
System.out.println("删除了" + count + "条记录");
} finally {
session.close();
}
}
@Test
public void testUpdateStudent() {
SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.getStudentById(2);
student.setName("小红");
student.setAge(20);
student.setGender(false);
student.setAddress("北京市海淀区");
int count = mapper.updateStudent(student);
session.commit();
System.out.println("更新了" + count + "条记录");
} finally {
session.close();
}
}
}
```
以上就是一个基于MyBatis注解的学生管理程序的示例,可以根据实际需求进行修改和扩展。
在eclipse中利用mybatis做一个学生查询系统
好的,我了解了您的问题。首先,您需要在Eclipse中创建一个Java项目。然后,您需要使用Maven将MyBatis引入您的项目中。您可以使用以下代码将MyBatis添加为Maven依赖项:
```xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
```
接下来,您需要创建一个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/mydb"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
```
在这个配置文件中,您需要指定数据库连接信息和映射器。随后,您需要创建一个Student类来映射数据库中的学生表,并创建一个StudentMapper接口来定义与学生表有关的操作。代码如下所示:
```java
public class Student {
private int id;
private String name;
private int age;
// getters and setters
}
```
```java
public interface StudentMapper {
Student selectStudentById(int id);
List<Student> selectAllStudents();
}
```
这里定义了两个方法,一个用于查询指定ID的学生,另一个用于查询所有学生。最后,您需要编写一个测试类来使用MyBatis进行学生查询操作。代码如下所示:
```java
public class TestMyBatis {
public static void main(String[] args) {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.selectStudentById(1);
System.out.println(student.getName() + " " + student.getAge());
List<Student> students = mapper.selectAllStudents();
for (Student s : students) {
System.out.println(s.getName() + " " + s.getAge());
}
session.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
```
在这个测试类中,首先我们需要读取MyBatis配置文件。接着,我们创建一个SqlSessionFactory,用于创建SqlSession。然后,我们从SqlSession中获取StudentMapper接口,并使用它查询指定ID的学生和所有学生。最后,我们关闭SqlSession。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)