配置文件内容主要是数据库的连接:postgres
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 顾名思义类型的别名,即 Person 代指com.sunmap.model.Person--> 6 <typeAliases> 7 <typeAlias alias="Person" type="com.sunmap.model.Person" /> 8 <typeAlias alias="Article" type="com.sunmap.model.Article" /> 9 </typeAliases> 10 11 <environments default="development"> 12 <environment id="development"> 13 <transactionManager type="JDBC" /> 14 <dataSource type="POOLED"> 15 <property name="driver" value="org.postgresql.Driver" /> 16 <property name="url" 17 value="jdbc:postgresql://192.168.5.11:5432/poidata" /> 18 <property name="username" value="postgres" /> 19 <property name="password" value="123456" /> 20 </dataSource> 21 </environment> 22 </environments> 23 24 <!-- --> 25 <mappers> 26 <mapper resource="com/sunmap/model/Person.xml" /> 27 </mappers> 28 29 </configuration>
预定义接口:
package com.sunmap.dao; import java.util.List; import com.sunmap.model.Article; import com.sunmap.model.Person; /** * * 定义的接口 * */ public interface IPersonOperation { public Person selectPersonByID(int id); public List<Person> selectPersonByName(String name); public void addPerson(Person p); public List<Person> selectPerson(); public void updatePerson(Person p); public void deletePersonById(Person p); public List<Article> getUserArticle(int id); }
定义的实体类Person:
package com.sunmap.model; public class Person { 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; } }
Person.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"> <!-- namespace 的作用,指向接口,即mybatis是面向接口编程,<select></select> 的id有指向接口的方法名; 个人理解<select></select> 就是接口的实现 --> <mapper namespace="com.sunmap.dao.IPersonOperation"> <!-- 定义返回类型为集合 --> <resultMap type="Person" id="resultListUser"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <!-- 关联查询 --> <resultMap type="Article" id="resultArticleList"> <id column="id" property="id" /> <result column="text" property="text" /> <association property="person" resultMap="resultListUser" column="" javaType="Person"> </association> </resultMap> <select id="getUserArticle" parameterType="int" resultMap="resultArticleList"> select person.id,person.name,person.age,article.id from person,article where person.id=article.personid and person.id=#{id} </select> <!-- 通过id查找 --> <select id="selectPersonByID" parameterType="int" resultType="Person"> select * from person where id = #{id} </select> <!-- 查询数据 --> <select id="selectPerson" resultType="Person"> select * from person </select> <select id="selectPersonByName" parameterType="String" resultMap="resultListUser"> select * from person where name like '%${_parameter}%'; </select> <!--插入数据 --> <insert id="addPerson" parameterType="Person"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> SELECT nextval('test_c_id_seq'::regclass) as id </selectKey> insert into person (id,name,age) values(#{id,jdbcType=INTEGER},#{name},#{age}) </insert> <!-- 更新 --> <update id="updatePerson" parameterType="Person"> update person set name =#{name},age = #{age} where id = #{id} </update> <!-- 删除 --> <delete id="deletePersonById" parameterType="Person"> delete from person where id = #{id} </delete> <!-- 关联查询 --> </mapper> <!-- <mapper namespace="com.sunmap.dao"> <select id="selectPersonByID" parameterType="int" resultType="Person"> select * from person where id = #{id} </select> <insert id="addPerson" parameterType="Person" useGeneratedKeys="true" keyProperty="id"> insert into Person (id,name,age) values(#{id},#{name},#{age}) insert into person (name,age) values(#{name},#{age}) </insert> </mapper> -->
测试代码:
package com.sunmap.dao; import static org.junit.Assert.*; 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.junit.Test; import com.sunmap.model.Article; import com.sunmap.model.Person; public class TestIPersonOperation { @Test public void test() { // fail("Not yet implemented"); try { Reader reader = Resources.getResourceAsReader("config/Configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); IPersonOperation iPersonOperation = session.getMapper(IPersonOperation.class); // List<Article> list = iPersonOperation.getUserArticle(1); // System.out.println(list.size()); /** * 删除 * */ // Person pp = new Person(); // pp.setId(2); // iPersonOperation.deletePersonById(pp); /** * 更新 * */ // Person pp = new Person(); // pp.setId(1); // pp.setName("update"); // pp.setAge(234); // iPersonOperation.updatePerson(pp); /** * 添加数据(主键id自动增长,这个的需要创建一个序列) * * CREATE SEQUENCE test_c_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1; ALTER TABLE test_c_id_seq OWNER TO postgres; 然后关联上 alter table test_c alter column id set default nextval('test_c_id_seq'); 这个块其实挺恶心的~ * */ // Person pp = new Person(); // pp.setName("sadfa"); // pp.setAge(24); // iPersonOperation.addPerson(pp); // // // //提交事物 session.commit(); //查询 // List<Person> personList = iPersonOperation.selectPerson(); // for(Person p:personList){ // System.out.println(p.getName()+" : "+p.getAge()); // } /** * 通过id查询 * */ // Person person = iPersonOperation.selectPersonByID(1); // System.out.println(person.getName()+":"+person.getAge()); /** * 模糊查询(返回List集合) * * * 这块有两个需要注意的地方 * * 1. '%${_parameter}%' ,单引号和#改成$ * 2. 获取参数时:${_parameter} * * 即:不管你的参数是什么,都要改成"_parameter"; * */ // List<Person> personList = iPersonOperation.selectPersonByName("sa"); // for(Person p:personList){ // System.out.println(p.getName()+" : "+p.getAge()); // } //关闭session session.close(); } catch (IOException e) { e.printStackTrace(); } } }