1.普通方法
在mybatis-config.xml文件里要进行注册
<mappers>
<mapper resource="static/PersonMapper.xml"/>
</mappers>
需要配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">
<mapper namespace="cn.kgc.idea_Mybatis.mapper.PersonMapper">
<insert parameterType="Person" id="savePerson">insert into tbl_person(`name`,age,birth,registerTime,salary) values(#{name},#{age},#{birth},#{registerTime},#{salary}) </insert>
<delete parameterType="int" id="deletePerson">DELETE FROM tbl_person WHERE id = #{id} </delete>
<update parameterType="Person" id="updatePerson">UPDATE tbl_person SET `name` = #{name},age = #{age},birth = #{birth},registerTime = #{registerTime},salary = #{salary} WHERE id = #{id} </update>
<select parameterType="int" id="getPersonById" resultType="Person">select * from tbl_person where id = #{id} </select>
<select id="getPersonAll" resultType="Person">select * from tbl_person </select>
</mapper>
需要有测试类
public class TestPersonMapper {
static SqlSessionFactory sqlSessionFactory;
static{
InputStream inputStream = null;
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static void main(String[] args) {
SqlSession session = sqlSessionFactory.openSession();
try {
PersonMapper personMapper = session.getMapper(PersonMapper.class);
/*//增加
Person person = new Person();
person.setName("张三");
person.setAge(24);
person.setBirth(new Date());
person.setRegisterTime(new Date());
person.setSalary(5000);
personMapper.savePerson(person);
session.commit();*/
/*//删除
personMapper.deletePerson(3);
session.commit();*/
/*//修改
Person person = new Person();
person.setName("王五");
person.setAge(24);
person.setBirth(new Date());
person.setRegisterTime(new Date());
person.setSalary(4000);
person.setId(2);
personMapper.updatePerson(person);
session.commit();*/
/*//查询一个
Person personById = personMapper.getPersonById(1);
System.out.println(personById);*/
//查询多个
List<Person> personAll = personMapper.getPersonAll();
for(Person p:personAll){
System.out.println(p);
}
} finally {
session.close();
}
}
}
2.注解方式
需要注册
举例:<mapper class="cn.kgc.idea_Mybatis.mapper.PersonMapperAnnotaion"/>
没有xml文件
sql语句写在接口里
public interface PersonMapperAnnotaion {
/*@Insert("insert into tbl_person(`name`,age,birth,registerTime,salary) " +
"values(#{name},#{age},#{birth},#{registerTime},#{salary})")*/
public void savePerson(Person person);
public void deletePerson(int id);
public void updatePerson(Person person);
public Person getPersonById(int id);
public List<Person> getPersonAll();
}
3.API方式
在测试类里直接写,需要借口,不需要xml文件
public class TestPersonMapperAPI {
static SqlSessionFactory sqlSessionFactory;
static{
InputStream inputStream = null;
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static void main(String[] args) {
SqlSession session = sqlSessionFactory.openSession();
try {
Person person = session.selectOne("cn.kgc.idea_Mybatis.mapper.PersonMapper.getPersonById", 1);
System.out.println(person);
} finally {
session.close();
}
}
}