mybatis 操作数据库,实现crud操作
延续mybatis 操作数据库(01),执行代码如下
1、mapper 类
<?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.mybatis01.mapper.personMapper"> <select id="queryPersonById" parameterType="int" resultType="com.mybatis01.mapper.Person"> select id,name,age from t_person where id = #{id} </select> <insert id="addPerson" parameterType="com.mybatis01.mapper.Person"> INSERT INTO t_person(id,name,age) VALUES (#{id},#{name},#{age}) </insert> <delete id="delStudentById" parameterType="int"> DELETE from t_person where id =#{id} </delete> <update id="updateStudentById" parameterType="com.mybatis01.mapper.Person"> UPDATE t_person SET NAME =#{name},age = #{age} where id =#{id} </update> <select id="queryListPerson" resultType="com.mybatis01.mapper.Person"> select id,name,age from t_person </select> </mapper>
2、测试类
import com.mybatis01.mapper.Person; 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.jupiter.api.Test; import java.io.Reader; import java.util.List; /** * @author :jack.zhao * @date :2021-10-16 22:42 */ public class test { /** * 根据ID查询人的信息 */ @Test public void test01() throws Exception{ // 加载MyBatis配置文件(访问数据库) // 将文件转换为对象过程 Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement = "com.mybatis01.mapper.personMapper.queryPersonById"; Person person = session.selectOne(statement,1001); System.out.println(person); session.close(); } // 查询集合信息 @Test public void selectList() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement = "com.mybatis01.mapper.personMapper."+"queryListPerson"; List<Person> listPerson = session.selectList(statement); System.out.println(listPerson); session.close(); } // 增加人员信息 @Test public void addPerson() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); Person persion = new Person(1003,"wangwu",29); String statement = "com.mybatis01.mapper.personMapper."+"addPerson"; int person = session.insert(statement,persion); session.commit(); // 因为配置的是JDBC方式需要手动执行commit操作 System.out.println("添加人员信息成功!"); session.close(); } // 根据ID删除指定信息 @Test public void delPesonByID() throws Exception{ Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement = "com.mybatis01.mapper.personMapper."+"delStudentById"; int person = session.delete(statement,1002); session.commit(); // 因为配置的是JDBC方式需要手动执行commit操作 System.out.println("删除人员信息成功!"); session.close(); } // 修改人员信息 @Test public void updateStudentById() throws Exception{ Person persion = new Person(1003,"chenqi",29); Reader reader = Resources.getResourceAsReader("mybatis-01.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement = "com.mybatis01.mapper.personMapper."+"updateStudentById"; int person = session.delete(statement,persion); session.commit(); // 因为配置的是JDBC方式需要手动执行commit操作 System.out.println("修改人员信息成功!"); session.close(); } }
3、实体类
package com.mybatis01.mapper; /** * @author :java.zhao * @date :2021-10-16 22:55 */ public class Person { public Person() { } public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } private int id; private String name; private int age; 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
4、配置文件
<?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="db.properties"/>--> <environments default="development"> <environment id="development"> <!-- 事务提交方式 JDBC :利用JDBC方式处理事务(commit rollback close) MANGED: 将失误交由其他组件去托管(spring jobss)--> <!--<transactionManager type="MANGED"/> <property value="false" name="closeConnection"/> --> <transactionManager type="JDBC"/> <!-- POOLED : 使用数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis01"/> <!--<property name="url" value="jdbc:mysql://127.0.0.1:3306/tx_db?characterEncoding=utf-8"/>--> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <!-- java.zhao 加载映射文件 --> <mapper resource="com/mybatis01/mapper/personMapper.xml"/> </mappers> </configuration>
5、pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>proxy-java</artifactId> <groupId>com.tycoon.netty</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.tycoon</groupId> <artifactId>mybatis-01</artifactId> <version>1.0-SNAPSHOT</version> <name>mybatis-01</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <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.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <filtering>false</filtering> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>