Mybatis(一)、mybatis环境搭建以及实体类及其配置
2017-03-13 10:14
429 查看
一、基础框架搭建
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>
相关文章推荐
- SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例
- MyBatis笔记---开发环境搭建、xml配置以及MybatisUtil工具类创建
- MyBatis框架环境搭建以及配置
- Hadoop平台搭建使用系列教程(5)- 网络以及初始统一环境配置
- ubuntu desktop下LAMP环境的搭建及其相关配置
- Hadoop平台搭建使用系列教程(5)- 网络以及初始统一环境配置
- Java开发环境搭建 Path以及Classpath环境变量的配置
- Linux环境下node.js环境的搭建以及配置,使用node.js编写工具与简化less编译
- JAVA学习(一):Java介绍及其平台、开发环境的配置与搭建
- 第一篇 PHP开发环境搭建以及多站点配置(基于windows 7系统)
- 基于SpringMVC、Maven以及Mybatis的环境搭建
- Win 8系统 Java开发环境搭建以及环境变量path的配置
- MyBatis环境搭建配置文件+入门视频下载
- (一)nodejs开发环境搭建: 安装nodejs以及配置npm
- PHP环境搭建:Windows 7下安装配置PHP+Apache+Mysql环境教程以及注意事项
- sublime Text 2 配置以及 Python环境搭建
- ubuntu 下lnmp环境搭建以及配置ngingx测试
- 基于maven的mybatis+springmvc环境搭建以及集成bootstraps
- MyBatis-环境配置以及查询
- Linux redis 3.2环境搭建以及开启启动配置