文件结构
暂时可以不去考虑与Blog相关的类,可以只考虑对person表的增删改。
1. 定义person的破pojo类:Person.java
package pht.mybatis.pojo;
import java.util.ArrayList;
import java.util.List;
public class Person {
private String personid;
private String name;
private String age;
private String address;
private String sex;
private List<Blog> blogs; //这个可以先不去理会
public String getPersonid() {
return personid;
}
public void setPersonid(String personid) {
this.personid = personid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return getName()+"---"+getAddress();
}
}
2. 定义映射接口PersonDao.java
package pht.mybatis.dao;
import java.util.HashMap;
import java.util.List;
import pht.mybatis.pojo.Person;
public interface Persondao {
public Person getById(String cid);
public List getAllperson();
public void addPerson(Person person);
public void updatePerson(Person person);
public void deletePerson(String cid);
}
3. 创建PersonDao.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="pht.mybatis.dao.Persondao">
<select id="getById" parameterType="java.lang.String" resultType="Person">
select * from person where personid=#{cid}
</select>
<select id="getAllperson" resultType="Person">
select * from person
</select>
<insert id="addPerson" parameterType="Person" useGeneratedKeys="true" keyProperty="personid">
insert into person(name,age,address,sex) values(#{name},#{age},#{address},#{sex})
</insert>
<update id="updatePerson" parameterType="Person">
update person set address=#{address} where personid=#{personid}
</update>
<delete id="deletePerson" parameterType="java.lang.String">
delete from person where personid=#{cid}
</delete>
</mapper>
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="config.properties"></properties><!-- 定义properties文件,该文件里有一些重要信息 -->
<settings>
<setting name="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="useGeneratedKeys" value="false" />
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25000" />
</settings>
<typeAliases>
<!--给实体类起一个别名 user -->
<typeAlias type="pht.mybatis.pojo.Person" alias="Person" />
<typeAlias type="pht.mybatis.pojo.Blog" alias="Blog" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" /><!-- ${}中的value取自properties文件中 -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="pht/mybatis/dao/Persondao.xml" />
<mapper resource="pht/mybatis/dao/BlogDao.xml" />
</mappers>
5. 测试类MyBatisTest.java
package pht.mybatis.test;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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 pht.mybatis.dao.*;
import pht.mybatis.pojo.*;
import pht.mybatis.sessionfactory.Sessionfactory;
/**
* myBatis数据库连接测试
*
*/
public class MyBatisTest {
private static SqlSession sqlSession;
public static void main(String[] args) {
sqlSession=Sessionfactory.SQLSESSIONFACTORY.getSessionFactory().openSession();
MyBatisTest test=new MyBatisTest();
try{
//这这里调用相应的方法
}catch (Exception e) {
// TODO: handle exception
}finally{
sqlSession.close();
}
}
public Person selectOne(String cid){
Persondao persondao = sqlSession.getMapper(Persondao.class);
Person p=persondao.getById(cid);
System.out.println(p);
return p;
}
public List selectMany(){
Persondao persondao = sqlSession.getMapper(Persondao.class);
List persons=persondao.getAllperson();
for(Iterator it=persons.iterator();it.hasNext();){
Person p=(Person) it.next();
System.out.println(p);
}
return persons;
}
public void insetPerson(Person p){
Persondao persondao = sqlSession.getMapper(Persondao.class);
persondao.addPerson(p);
sqlSession.commit();
}
public void updatePerson(){
Persondao persondao = sqlSession.getMapper(Persondao.class);
Person ps=persondao.getById("4");//先得到要修改的记录,再进行修改
ps.setAddress("CHINA");
persondao.updatePerson(ps);
sqlSession.commit();
}
public void deletePerson(String cid){
Persondao persondao = sqlSession.getMapper(Persondao.class);
persondao.deletePerson(cid);
sqlSession.commit();
}
}