学习Mybatis中数据库操作的初次使用
一、下载mybatis.jar包,导入项目。
二、创建实体类:org.ruangong.entity。
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
<?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,配置数据库连接
<?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>
五、实现数据库操作:
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(); } }
收获:在传统连接数据库上,用到了连接池提高了数据库操作性能。