mybatis第3天

来源:互联网 发布:visio2007网络图形包 编辑:程序博客网 时间:2024/05/01 12:19
<mapper>

•<!--连接ORM映射文件-->
  <mapper resource="com/lovo/bean/User.xml"/>
•<!--配置ORM映射接口,使用注解配置-->
  <mapper class="com.lovo.dao.IUserDao"/>

<!-- 添加 操作 -->
<!--id为引用名,parameterType为参数类型-->
<insert id="insert"  parameterType="User"   useGeneratedKeys="true"  keyProperty="id">
<!--#{}表示取出参数对象中对应的属性值-->
    insert into t_man(user_name,user_phone)values(#{user_name},#{user_phone})</insert>
useGeneratedKeys表示执行完添加语句后可以返回自动增长列的主键值 
keyProperty表示将返回的主键值填充当前对象的id属性
</mapper>

#在生成SQL时,对于字符类型参数,会拼装引号
$在生成SQL时,不会拼装引号,可用于order by之类的参数拼装
<select id="findByName" resultMap="userMap">
  select * from t_user where u_userName like '%${userName}%';
  </select>
List list = session.selectList("user.findByName", map);


批量删除

<deleteid="delMore">

  delete from t_user where u_id in(

  <foreach collection="ids"item="id" separator=",">

  #{id}

  </foreach>

  )

  </delete>

collection为用于遍历的元素(必选),支持数组、List、Set
item表示集合中每一个元素进行迭代时的别名.
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置.
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔符.
close表示以什么结束.


当查询条件需要动态生成时,需要使用动态SQL

<selectid="dynaFindUser" resultMap="user">

  select * from t_user where 1=1

  <iftest="id != 0">

  and u_id > #{id}

  </if>

  <if test="userName != null and userName != ''">

  and u_userName like '%${userName}%'

  </if>

  </select>

List list= session.selectList("dynaFindUser",newUserBean(0,"张"));


<select id="findByCount" parameterType="User" resultType="Integer">
<![CDATA[
select count(*) from tm_user
]]>
<trim prefix="where" prefixOverrides="and|or">   <!--插入where语句 ,自动判断进行条件拼接-->
<include refid="whereColumns"/><!--引入where语句-->
</trim>
</select>


一对多:

类定义

一方:

publicclass UserObj {

   privateint id;

   privateString name;

  private int age;

 

  private List<ResultObj> resultList = newArrayList();

}

多方:

publicclass ResultObj {

   privateint id;

   privateint point;

     private UserObj user;


<!-- 定义类别名 -->

 <typeAlias alias="users"type="com.map.UserObj"/>

 <typeAlias alias="results"type="com.map.ResultObj"/>


 <!-- 定义按用户ID查询用户。里面包括成绩集合 -->

 <select id="findById" resultMap="user_Result">

   select * from t_user where t_id=#{id}

 </select>


定义用户结果集
  <!--用户表类属性映射,extends表示可继承userMap所定义的映射 -->
  <resultMap id="user_Result" type="user" extends="userMap">
    
   <!--属性名resultList 类型为List集合 该集合是根据UserObj的t_id进行ResultObj查询
          column是本表要传入的select语句块中的参数,集合查询的定义为resultByUid -->
    <association property="resultList" column="t_id"select="resultByUid"/>
  </resultMap>



多对多映射:

操作多对多关系中,中间表需要手动维护
类定义
老师表

publicclass TeacherBean {

  private int id;

  private String teacherName;

  private List studentList;

}

学生表

publicclass StudentBean {

  private int id;

  private String studentName;

   private List teacherList;

}


一对多,多对多,增,删,改,查的操作

mybatis和spring整合

mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
spring核心包
spring AOP包
spring ORM包

配置数据源连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />  驱动
  <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&amp;allowMultiQueries=true"></property>URL
                <property name="username" value="root" />用户名
                <property name="password" value="lovo" />密码
最大连接数
                <property name="maxActive" value="50"></property>
最小连接数
                <property name="maxIdle" value="20"></property>
                <property name="maxWait" value="1000"></property>等待时间
</bean>

配置会话工厂
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property><!--数据源-->
                  <!--mybatis配置文件路径-->
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
                      <!--自动扫描指定路径下配置文件-->
<property name="mapperLocations">
<list>
<value>classpath:orm/*.xml</value>
</list>
</property>
</bean>


配置事务管理器
<bean id="trans" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>


配置使用注解完成事务
<tx:annotation-driven transaction-manager="trans"/>


0 0
原创粉丝点击