mybatis一对多配置问题
来源:互联网 发布:dns 一个域名多个ip 编辑:程序博客网 时间:2024/05/16 05:01
1.数据库
user:
role:
2.实体类
User:
public class User {private int userId;private String userName;private String password;private String email;private Role role;public Role getRole() {return role;}public void setRole(Role role) {this.role = role;}//private int type;public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}/*public int getType() {return type;}public void setType(int type) {this.type = type;}*/}
Role:
public class Role {private int roleId;private String roleName;public int getRoleId() {return roleId;}public void setRoleId(int roleId) {this.roleId = roleId;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}}
3.映射文件mapper
最为重要的一部分
UserMap.xml
其中:resultMap对应的为在xml文件定义的resultMap id="xxx"
resultType则是写实体类
association负责进行自动查询相关联的表
property:与实体类中定义的属性相对应
colump:则与数据库中表的外键名相对应
javaType:相关联的实体类
select:查询语句的id
<pre name="code" class="plain"><?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="com.ssm.mapper.UserMapper" ><resultMap id="user" type="com.ssm.model.User"><id property="userId" column="UserID"/><result property="userName" column="UserName"/><result property="password" column="PassWord"/><association property="role" column="Type" javaType="com.ssm.model.Role" select="getRoleForID"><result property="email" column="Email"/></association></resultMap><select id="selectUserByModel" parameterType="com.ssm.model.User" resultMap="user">select * from user where UserName=#{user.userName} and PassWord=#{user.password}</select>
RoleMap.xml
<pre name="code" class="plain"><?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="com.ssm.mapper.UserMapper" ><resultMap id="role" type="com.ssm.model.Role"><id property="roleId" column="RoleID"/><result property="roleName" column="RoleName"/></resultMap><select id="getRoleForID" parameterType="int" resultType="com.ssm.model.Role">select *from rolewhere RoleID=#{roleid}</select></mapper>4.接口该接口不用实现即可完成dao层功能使用注解注入
@Repository(value = "userMapper") public interface UserMapper{
User selectUserByModel(@Param(value = "user") User user); Role getRoleForID(@Param(value = "roleid")int roleid);}5.配置aapplicationContext.xml中的配置直接对目录进行扫描省去了将每一个mapper配置文件一一配置的麻烦
<pre name="code" class="plain"><!-- 创建SqlSessionFactory,同时指定数据源 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath:com/ssm/mapping/*.xml"></property></bean>
讲的不专业,自己看着玩
0 0
- mybatis一对多配置问题
- mybatis一对多配置
- mybatis 一对多的配置 和 查询数据覆盖问题
- mybatis一对多查询配置
- MyBatis 一对多映射配置
- mybatis一对多的配置
- mybatis配置一对多关系
- MyBatis一对多问题记录
- mybatis关联配置(一对多配置)
- mybatis 一对多,多对一配置
- mybatis 一对多,多对一配置
- spring+mybatis 一对多配置总结
- Mybatis一对一和一对多配置
- Mybatis(ibatis)的一对多关联配置
- mybatis复杂一对多映射配置示例
- Mybatis的一对多查询中的问题
- Mybatis一对多返回结果问题
- iBATIS一对多配置问题
- LoadRunner检查点使用小结
- TOP N (java)
- 02 自定义网站的根目录
- friend class & function
- MATLAB画图-legend,box用法
- mybatis一对多配置问题
- Loadrunner集合点Rendezvous知识
- 称砝码
- viewPager和listview嵌套冲突
- flask之行(flask环境安装)
- Linux内核-menuconfig和kconfig和.config的关系
- poj 1700 Crossing River
- NSFileHandle的使用
- hdu5375 dp+递推