学习Mybatis中数据库操作的初次使用
一、下载mybatis.jar包,导入项目。
二、创建实体类:org.ruangong.entity。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package org.ruangong.entity; import org.apache.ibatis.reflection.SystemMetaObject; public class Person { private int id; public int getId() { return id; } public void setId( int id) { this .id = id; } public Person() { } public Person( int id, String name, int age) { super(); this .id = id; this .name = name; this .age = age; } 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; } private String name; private int age; @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]" ; } } |
三、创建personMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?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 = "org.ruangong.mapper.personMapper" > < select id= "queryPersonById" resultType= "org.ruangong.entity.Person" parameterType= "int" > select * from person where id = #{id} </ select > <insert id= "add" parameterType= "org.ruangong.entity.Person" > insert into person(id,name,age) value(#{id},#{name},#{age}) </insert> <delete id= "delete" parameterType= "int" > delete from person where id = #{id} </delete> < select id= "queryall" resultType= "org.ruangong.entity.Person" > select * from person </ select > <update id= "updatePerson" parameterType= "org.ruangong.entity.Person" > update person set name=#{name},age=#{age} where id=#{id} </update> </mapper> |
四、创建配置文件conf.xml,配置数据库连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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= "${driver}" /> <property name= "url" value= "${url}" /> <property name= "username" value= "${username}" /> <property name= "password" value= "${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource= "org/ruangong/mapper/personMapper.xml" /> </mappers> </configuration> |
五、实现数据库操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package org.ruangong.test; import java.io.IOException; import java.io.Reader; import java.util.List; 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.ruangong.entity.Person; import org.ruangong.mapper.personMapper; public class TestMybatis { public static void queryOnePerson() throws IOException{ //加载配置文件 Reader reader = Resources.getResourceAsReader( "conf.xml" ); //SqlsessionFactory -- conection SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement= "org.ruangong.mapper.personMapper.queryPersonById" ; Person person = session.selectOne(statement,1); System. out .println(person.getAge()+ "," +person.getId()+ "," +person.getName()); session.close(); } public static void addOnePerson() throws IOException{ //加载配置文件 Reader reader = Resources.getResourceAsReader( "conf.xml" ); //SqlsessionFactory -- conection SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement= "org.ruangong.mapper.personMapper.add" ; Person person= new Person(2, "疯狗伟" ,80); int count = session.insert(statement,person); System. out .println( "增加行数:" +count); session.commit(); session.close(); } public static void deleteOnePerson() throws IOException{ //加载配置文件 Reader reader = Resources.getResourceAsReader( "conf.xml" ); //SqlsessionFactory -- conection SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement= "org.ruangong.mapper.personMapper.delete" ; int count = session.delete(statement,2); System. out .println( "删除行数:" +count); session.commit(); session.close(); } public static void queryAllPerson() throws IOException{ //加载配置文件 Reader reader = Resources.getResourceAsReader( "conf.xml" ); //SqlsessionFactory -- conection SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement= "org.ruangong.mapper.personMapper.queryall" ; List<Person> list = session.selectList(statement); System. out .println(list); session.close(); } public static void updatePerson() throws IOException{ //加载配置文件 Reader reader = Resources.getResourceAsReader( "conf.xml" ); //SqlsessionFactory -- conection SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String statement= "org.ruangong.entity.personMapper.updatePerson" ; Person person = new Person(); person.setId(1); person.setName( "疯狗伟" ); person.setAge(30); session.update(statement, person); session.commit(); session.close(); } public static void main(String[] args) throws IOException { //queryOnePerson(); //addOnePerson(); //deleteOnePerson(); // queryAllPerson(); // updatePerson(); queryPerson2(); // addPerson2(); } } |
收获:在传统连接数据库上,用到了连接池提高了数据库操作性能。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步