mybaits中每个实体对象都对应一个Mapper文件,因此在mapper中配置相关的增删改查语句
<?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="mapping.studentMapper">
<!-- 指定查询SQL, parameterType是传入参数类型,resultType是返回结果-->
<select id="getStudent" parameterType="int" resultType="dto.student">
SELECT * FROM student WHERE id=#{id}
</select>
<!-- 添加 -->
<insert id="addStudent" parameterType="dto.student">
INSERT INTO student(name,code) VALUES (#{Name},#{Code})
</insert>
<!-- 删除 -->
<delete id="delStudent" parameterType="java.lang.String">
DELETE FROM student WHERE Code=#{_parameter}
</delete>
<!-- 修改 -->
<update id="updateStudent" parameterType="dto.student">
UPDATE student SET name=#{Name} WHERE Code=#{Code}
</update>
<!-- 查询全部 -->
<select id="getList" resultType="dto.student">
SELECT Id, Name, Code FROM student
</select>
</mapper>
以上包括基本的增删改查和查询集合。其中parameterType是传入的参数类型,resultType是返回的结果类型,注意单个查询和查询所有结果,resultType的值是一样的
操作Mapper文件
@Controller
@RequestMapping("/Home")
public class HomeController {
@Resource(name="applePhone")
private IMobilePhone phone;
@RequestMapping(value="index")
public String Index()
{
String msg = phone.PhoneBrand();
System.out.print(msg);
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.studentMapper.getStudent"; //在studentMapper.xml中有命名空间+方法名
Student student= session.selectOne(statesql, 1);
System.out.println(student.getName());
return "index";
}
@RequestMapping(value="AddStudent")
public String AddStudent()
{
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.studentMapper.addStudent";//在studentMapper.xml中有命名空间+方法名
Student student=new Student();
student.setName("PG ONE");
student.setCode("三年二班");
int result = session.insert(statesql,student);
session.commit();
session.close();
System.out.println(result);
return "index";
}
@RequestMapping(value="Delstudent")
public String Delstudent()
{
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.studentMapper.delStudent";//在studentMapper.xml中有命名空间+方法名
int result = session.delete(statesql,"00002");
session.commit();
session.close();
System.out.println(result);
return "index";
}
@RequestMapping(value="Updatestudent")
public String Updatestudent()
{
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.studentMapper.updateStudent";//在studentMapper.xml中有命名空间+方法名
student student=new student();
student.setName("GAI");
student.setCode("00002");
int result = session.update(statesql, student);
session.commit();
session.close();
System.out.println(result);
return "index";
}
@RequestMapping(value="List")
public String List()
{
String resource = "/conf.xml";
//加载mybatis的配置文件
InputStream inputstream =this.getClass().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
SqlSession session = sessionFactory.openSession();
String statesql= "mapping.studentMapper.getList"; //在studentMapper.xml中有命名空间+方法名
java.util.List<student> listResult= session.selectList(statesql);
session.commit();
session.close();
for(dto.student item:listResult)
{
System.out.print(item.getId()+"--"+item.getCode()+"--"+item.getName());
System.out.print("--------------");
}
return "index";
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。