MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架
mybatis 使用
mybatis 配置文件 :
<configuration>
<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/integraimanager" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<!-- 映射文件-->
<mappers>
<mapper resource="com/wisezone/mybatis/demo1/model/UserMapper.xml" />
</mappers>
</configuration>
映射文件配置:
<!-- 命名空间 如果是接口实现 与接口包名+类名 路径相同 --->
<mapper namespace="com.wisezone.mybatis.demo1.UserMapper">
<resultMap id="userResult" type="com.wisezone.mybatis.demo1.model.User">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="password" column="password" jdbcType="VARCHAR"/>
</resultMap>
<!--parameterType属性指明查询时使用的参数类型 resultType属性指明查询返回的结果集类型-->
<insert id="insertUser" parameterType="com.wisezone.mybatis.demo1.model.User" useGeneratedKeys="true" keyProperty="id">
insert into adminuser (username,password)values(#{username},#{password})
</insert>
</mapper>
接口实现:
id名与接口中方法名相同
java 接口 :
@Repository("adminDao")
public interface AdiminDao {
public List<Admin> seleAll();
public List<Permissions> seleAllPer();
Permissions seleByname(String name);
}
映射文件:
<mapper namespace="com.hst.mybatis.dao.AdiminDao">
<resultMap type="com.hst.mybatis.model.Permissions" id="permissionsList">
<id property="id" column="id" jdbcType="INTEGER" />
<result property="pname" column="pname" jdbcType="VARCHAR" />
</resultMap>
<resultMap type="com.hst.mybatis.model.Admin" id="adminResut">
<id property="id" column="id" jdbcType="INTEGER" />
<result property="name" column="name" jdbcType="VARCHAR" />
<result property="age" column="age" jdbcType="INTEGER" />
<result property="sex" column="sex" jdbcType="INTEGER" />
<result property="headerUri" column="headUri" jdbcType="VARCHAR" />
<association property="permissions" resultMap="permissionsList"/>
</resultMap>
<select id="seleAll" resultMap="adminResut">
SELECT p.*,u.* FROM permissions AS p,user AS u WHERE p.id=u.pid
</select>
Spring 与 mybatis 整合 :
引入 jar包
<!-- spring spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
spring -mybatis 配置文件:
主要配置:
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/hst/integraimanager/mapper/*.xml" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hst.integraimanager.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
参数传递:
对象 ,基本类型 ,map集合多个参数的传递:
<update id="dropAdminUserDao" parameterType="map">
UPDATE adminuser SET UserName=#{user.username} , PassWord=#{user.password} WHERE
UserName=#{username}
</update>
map 传递参数:
public void testupodate() {
AdminUser a=new AdminUser("s","s","1997-01-02","00");
Map map= new HashMap<>();
map.put("username", "a");
map.put("user", a);
adminUserMapper.dropAdminUserDao(map);
}
多个参数传递 :使用param 接收参数
AdminUser a=adminUserMapper.loginDao("a", "sad");
<select id="loginDao" resultMap="BaseResultMap">
SELECT * FROM adminuser WHERE
binary username=#{param1} AND passWord=#{param2}
</select>
Mybatis 关联查询
多对一 一对一 使用 association
一对多 多对多 使用 collection
column= 数据库外键 一对多 || 一对一
<resultMap id="memResultBase" type="com.hst.integraimanager.model.Membership">
</resultMap>
<resultMap id="historyResult" type="com.hst.integraimanager.model.ChangeHistory">
<association property="membership" resultMap="memResultBase" />
</resultMap>
动态 sql 语句查询 :
1. if 语句 (简单的条件判断)2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
5. set (主要用于更新时)
6. foreach (在实现 mybatis in 语句查询时特别有用)