Mybatis(一)、mybatis环境搭建以及实体类及其配置

    xiaoxiao2021-03-25  81

    一、基础框架搭建

    1、引入mybatisjar包

    2、加载configuration配置文件,命名resource.xml

    resource.xml配置文件代码展示 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases><!--指定使用到的数据类型别名 --> <typeAlias alias="string" type="java.lang.String" /> <typeAlias alias="list" type="java.util.List" /> <typeAlias alias="student" type="com.lattice.student.entity.StudentEntity" /> <typeAlias alias="teacher" type="com.lattice.teacher.entity.TeacherEntity" /> </typeAliases> <environments default="development"><!--配置数据库连接 --> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatic?characterEncoding=utf-8" /> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers><!--加载实体类配置文件 --> <mapper resource="com/lattice/student/entity/StudentEntity.xml"/> <mapper resource="com/lattice/teacher/entity/TeacherEntity.xml"/> </mappers> </configuration>

    二、将配置文件加载到项目中

    1、选中项目,右键,点properties,再选java buildPath  then  add folder。

    2、加载jar包,右键,点properties,再选java buildPath  libraries  then  add external jars 。

    三、创建实体类及实体类配置

    1、根据数据库中对应的表,创建StudentEntity.java,及StudentEntity.xml

    public class StudentEntity implements Serializable { private static final long serialVersionUID = 1L; private String s_id; private String s_name; private int s_age; private String s_loginName; private String s_passWord; private TeacherEntity teacherEntity; public String getS_id() { return s_id; } public void setS_id(String s_id) { this.s_id = s_id; } public String getS_name() { return s_name; } public void setS_name(String s_name) { this.s_name = s_name; } public int getS_age() { return s_age; } public void setS_age(int s_age) { this.s_age = s_age; } public String getS_loginName() { return s_loginName; } public void setS_loginName(String s_loginName) { this.s_loginName = s_loginName; } public String getS_passWord() { return s_passWord; } public void setS_passWord(String s_passWord) { this.s_passWord = s_passWord; } public TeacherEntity getTeacherEntity() { return teacherEntity; } public void setTeacherEntity(TeacherEntity teacherEntity) { this.teacherEntity = teacherEntity; } @Override public String toString() { return "StudentEntity [s_id=" + s_id + ", s_name=" + s_name + ", s_age=" + s_age + ", s_loginName=" + s_loginName + ", s_passWord=" + s_passWord + ", teacher=" + teacherEntity.getT_name() + "]"; } }

    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="student"> <!--表示结果集,id表示结果集名称 type表示结果集的类型 这里的student表示别名 可以使用别名也可以使用类地址 --> <resultMap id="student" type="student" > <id property="s_id" column="s_id" /><!-- 主键 --> <result property="s_name" column="s_name" /><!-- 其他列名 --> <result property="s_age" column="s_age" /><!-- 其他列名 --> <result property="s_loginName" column="s_loginName" /><!-- 其他列名 --> <result property="s_passWord" column="s_passWord" /><!-- 其他列名 --> <association property="teacherEntity" column="t_id" select="teacher.selectTeacher"></association> </resultMap> <!--通过老师T_id查询队形的学生,来为教师实体类的学生对象列表的赋值,以此完成在查询到这个教师对应的所有学生--> <select id="selectStudentByT_id" resultMap="student" parameterType="string"> select * from student where t_id=#{t_id} </select> </mapper>

    2、创键TeacherEntity.java及TeacherEntity.xml

    public class TeacherEntity { private String t_id; private String t_name; private String t_age; private List studentEntityList;//配置与student表的一对多的级联关系 public String getT_id() { return t_id; } public void setT_id(String t_id) { this.t_id = t_id; } public String getT_name() { return t_name; } public void setT_name(String t_name) { this.t_name = t_name; } public String getT_age() { return t_age; } public void setT_age(String t_age) { this.t_age = t_age; } public List getStudentEntityList() { return studentEntityList; } public void setStudentEntityList(List studentEntityList) { this.studentEntityList = studentEntityList; } } <!-- TeacherEntity.xml --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="teacher"> <!--表示结果集,id表示结果集名称 type表示结果集的类型 这里的student表示别名 可以使用别名也可以使用类地址 --> <resultMap id="teacher" type="teacher" > <id property="t_id" column="t_id" /><!-- 主键 --> <result property="t_name" column="t_name" /><!-- 其他列名 --> <result property="t_age" column="t_age" /><!-- 其他列名 --> <collection property="studentEntityList" column="t_id" select="student.selectStudentByT_id"></collection> </resultMap> <select id="selectTeacher" resultMap="teacher" parameterType="string"> select * from teacher where t_id=#{t_id} </select> </mapper>

    转载请注明原文地址: https://ju.6miu.com/read-39182.html

    最新回复(0)