demo整体框架

studentMapper.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是该mapper.xml的唯一标识 -->
<mapper namespace="Entity.studentMapper">
<!-- 通过namespace和id定位sql -->
<!-- parameterType输入参数类型 resultType返回结果值类型 -->
<select id="queryStudentByStudentStuNo" parameterType="int"
resultType="Entity.Student">
select * from student where stuNo = #{stuNo}
</select>
<insert id="addStudent" parameterType="Entity.Student">
insert into student(stuNo,stuName,stuAge,graName)
values(#{stuNo},#{stuName},#{stuAge},#{graName})
</insert>
<update id="updateStudentByStuNo" parameterType="Entity.Student">
update student set stuName = #{stuName},stuAge = #{stuAge},graName =
#{graName} where stuNo = #{stuNo}
</update>
<delete id="deleteStudentByStuNo" parameterType="int">
delete from student where stuNo = #{stuNo}
</delete>
<select id="queryAllStudent" resultType="Entity.Student">
select * from student
</select>
</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值和environment的id值来指定Mybatis运行时的数据库环境 -->
<environments default="development">
<!-- 开发环境 -->
<environment id="development">
<!-- 事物提交方式: JDBC:利用JDBC方式处理事物(commit rollback close) MANAGED:将事物交由其他组件托管(spring,jobss),默认会关闭连接
<property name="closeConnection" value="false"/> -->
<transactionManager type="JDBC" />
<!-- 数据源类型: UNPOOLED:传统的JDBC模式(每次访问数据库均需要打开,关闭数据库操作,打开关闭数据库消耗性能) POOLED:使用数据库连接池(第三方)
JNDI:从tomcat中获取一个内置的数据库连接池 -->
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test" />
<property name="username" value="sa" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载映射文件 -->
<mapper resource="Entity/studentMapper.xml" />
</mappers>
</configuration>
Student.java
package Entity;
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
private String graName;
public Student() {
}
public Student(int stuNo, String stuName, int stuAge, String graName) {
super();
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
this.graName = graName;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getGraName() {
return graName;
}
public void setGraName(String graName) {
this.graName = graName;
}
@Override
public String toString() {
return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuAge=" + stuAge + ", graName=" + graName + "]";
}
}
Test.java
package Entity;
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;
public class Test {
// 查询单个学生
public static void queryStudentByStuNO() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
// session相当于connection
SqlSession session = sessionFactory.openSession();
String statement = "Entity.studentMapper.queryStudentByStudentStuNo";
Student student = session.selectOne(statement, 1);
System.out.println(student);
session.close();
}
// 查询所有学生
public static void queryAllStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
// session相当于connection
SqlSession session = sessionFactory.openSession();
String statement = "Entity.studentMapper.queryAllStudent";
List<Student> students = session.selectList(statement);
System.out.println(students);
session.close();
}
// 增加学生
public static void addStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
// session相当于connection
SqlSession session = sessionFactory.openSession();
String statement = "Entity.studentMapper.addStudent";
Student student = new Student(4, "zl", 11, "z");
int count = session.insert(statement, student);
session.commit();// 提交事物
System.out.println("增加" + count + "学生");
session.close();
}
// 删除学生
public static void deleteStudentByStuNo() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
// session相当于connection
SqlSession session = sessionFactory.openSession();
String statement = "Entity.studentMapper.deleteStudentByStuNo";
int count = session.delete(statement, 4);
session.commit();// 提交事物
System.out.println("删除" + count + "学生");
session.close();
}
// 修改学生信息
public static void updateStudentByStuNo() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
// session相当于connection
SqlSession session = sessionFactory.openSession();
String statement = "Entity.studentMapper.updateStudentByStuNo";
Student student = new Student();
student.setStuNo(3);
student.setStuName("wlw");
student.setStuAge(11);
student.setGraName("w");
int count = session.update(statement, student);
session.commit();
session.close();
}
public static void main(String[] args) throws IOException {
// queryStudentByStuNO();
// addStudent();
// deleteStudentByStuNo();
// updateStudentByStuNo();
// queryAllStudent();
}
}
本文详细介绍了一个基于MyBatis框架的学生信息管理系统实现过程,包括数据库配置、Mapper文件定义、实体类设计及各种CRUD操作的执行。
2415

被折叠的 条评论
为什么被折叠?



