Mybatis学习笔记-Mybatis高级应用

  • 知识回顾
  1. log4j日志没有显示

配置环境升级jdk1.6升级为jdk1.7;(log4j-1.2.17.jar)

 

  1. mybatis作用

持久层框架

特点:半自动ORM映射,将数据库中的数据封装到对象中

 

  1. 怎么获得mybatis?

SqlSessionFactory 线程安全性

SqlSession 线程非安全,不能做类的公用变量

 

4)Mybatis核心配置文件 sqlMapConfig.xml

配置内容:

  1. 配置事务
  2. 配置数据源
  3. 声明mapper文件

 

5)Mapper文件

  1. 命名空间,在不同的mapper,但mapper中的方法同名,靠命名空间区分
  2. resultMap不是必须,可以不写,前提:表和实体的属性一样。(mybatis底层自动生成resultMap)
  3. 它通过数据结果集来映射的。Hibernate它是通过数据库表字段来映射的。
  4. 关联关系:

A.对一association  javaType

B.对多 collection   ofType

jdbcType 

它是指定当NAME为null时,给jdbc驱动程序告诉它,它针对的数据库字段的类型。(Oracle)

       

                 update person

                

                       user_name=#{name,jdbcType=VARCHAR},

                         age=#{age},

                         remark=#{remark},

                

                 where id = #{id}

       

 

6)Mapper中写SQL

获取参数

#{} 它会自动根据参数类型做封装。例如对字符串类型,两边加单撇;对整数类型直接使用;好处防止SQL注入(推荐)

 

${} 将用户填入的参数直接拼接到SQL。(字符串直接拼接)坏处:SQL注入;例如:拼接order by条件(特殊的地方)

 

7)SQL中含有特殊字符

#{ageEnd}

 

8)动态SQL

  判断条件

自动删除最前面的and 或者or,实际开发中常用where 1=1

它自动删除最后一个set值的逗号,修改时,修改参数不同

主要应用在in子查询,批量删除时候,array,list,map

 

9)常规的标签

                 select * from person

                 where 1=1

                 and user_name like #{name}

                 and age>#{ageStart}

                

       

 

  1. 创建Mapper接口

package cn.itcast.ssm.mapper;

 

import java.util.List;

import java.util.Map;

 

import cn.itcast.ssm.domain.Person;

 

/**

 * @Description:

 * @Author:            传智播客 java学院       陈子枢

 * @Company:       http://java.itcast.cn

 * @CreateDate:    2014年11月17日

 */

public interface PersonMapper {

        public List find(Map map);

}

  1. 创建PersonDao接口

package cn.itcast.ssm.dao;

 

import java.util.List;

import java.util.Map;

 

import cn.itcast.ssm.domain.Person;

 

/**

 * @Description:

 * @Author:            传智播客 java学院       陈子枢

 * @Company:       http://java.itcast.cn

 * @CreateDate:    2014年11月17日

 */

public interface PersonDao {

 

        public List find(Map map);

 

}

  1. 创建PersonDaoImpl

package cn.itcast.ssm.dao.impl;

 

import java.util.List;

import java.util.Map;

 

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import org.springframework.stereotype.Repository;

 

import cn.itcast.ssm.domain.Person;

import cn.itcast.ssm.mapper.PersonMapper;

 

/**

 * @Description:PersonDao

 * @Author:            传智播客 java学院       陈子枢

 * @Company:       http://java.itcast.cn

 * @CreateDate:    2014年11月17日

 */

@Repository

public class PersonDaoImpl extends SqlSessionDaoSupport {

       

        /*

         * mybatis3.0.0+mybatis-psring1.0.0无需,整合包自己注入

         * mybatis3.2.2+mybatis-spring1.2.0 必须自己注入sqlSessionFactory;

         */

        public void setSqlSessiionFactory(SqlSessionFactory sqlSessiionFactory) {

                 super.setSqlSessionFactory(sqlSessiionFactory);

        }

       

        public List find(Map map){

                 PersonMapper mapper = this.getSqlSession().getMapper(PersonMapper.class);

                 return mapper.find(map);

        }

       

}

  1. 创建PersonService接口

package cn.itcast.ssm.service;

 

import java.util.List;

import java.util.Map;

 

import cn.itcast.ssm.domain.Person;

 

/**

 * @Description:

 * @Author:            传智播客 java学院       陈子枢

 * @Company:       http://java.itcast.cn

 * @CreateDate:    2014年11月17日

 */

public interface PersonService {

 

        public List find(Map map);

 

}

  1. 创建PersonServiceImpl

package cn.itcast.ssm.service.impl;

 

import java.util.List;

import java.util.Map;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Service;

 

import cn.itcast.ssm.dao.PersonDao;

import cn.itcast.ssm.domain.Person;

import cn.itcast.ssm.service.PersonService;

 

/**

 * @Description:

 * @Author:            传智播客 java学院       陈子枢

 * @Company:       http://java.itcast.cn

 * @CreateDate:    2014年11月17日

 */

@Service

public class PersonServiceImpl implements PersonService {

        @Resource

        PersonDao personDao;

       

        public List find(Map map){

                 return personDao.find(map);

        }

}

  1. 创建PersonController

package cn.itcast.ssm.web.controller;

 

import java.util.List;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

 

import cn.itcast.ssm.domain.Person;

import cn.itcast.ssm.service.PersonService;

 

/**

 * @Description:

 * @Author:            传智播客 java学院       陈子枢

 * @Company:       http://java.itcast.cn

 * @CreateDate:    2014年11月17日

 */

@Controller

public class PersonController {

        @Resource

        PersonService personService;

       

        //查询,将查询结果传递到页面

        @RequestMapping("/person/list.action")      //Controller加载时会自动创建访问路径 /person/list;/person/list.action

        public String list(Model model){

                 List personList = personService.find(null);

                 model.addAttribute("personList", personList);               //传递到页面

                

                 return "/person/jPersonList.jsp";

        }

}

  1. 创建列表页面jPersonList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 

    My JSP 'jPersonList.jsp' starting page

 

 

 

    人员列表

 

       

       

       

       

 

       

       

       

       

 

序号 姓名 年龄 备注
${status.index+1} ${p.name} ${p.age} ${p.remark}
   

   

 

  1. 配置sqlMapConfig.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

 

  1. 配置beans.xml

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:context="http://www.springframework.org/schema/context"

        xmlns:aop="http://www.springframework.org/schema/aop"

        xmlns:tx="http://www.springframework.org/schema/tx"

        xsi:schemaLocation="http://www.springframework.org/schema/beans

                                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                                           http://www.springframework.org/schema/context

                                           http://www.springframework.org/schema/context/spring-context-3.0.xsd

                                           http://www.springframework.org/schema/tx

                                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

                                           http://www.springframework.org/schema/aop

                                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

       

       

       

       

       

       

       

       

       

                

                

                

                

       

       

       

       

                

                

                

              

                

                

                

       

       

       

       

                

       

       

                

                        

                        

                        

                        

                        

                        

                        

                        

                        

                        

                

       

       

                

                

       

       

                                 

  1. 配置springmvc-servlet.xml

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:mvc="http://www.springframework.org/schema/mvc"

        xmlns:context="http://www.springframework.org/schema/context"

        xmlns:aop="http://www.springframework.org/schema/aop"

        xmlns:tx="http://www.springframework.org/schema/tx"

        xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

       

       

       

       

       

       

                

                

       

       

  1. 配置web.mxl

                 xmlns="http://java.sun.com/xml/ns/javaee"

                 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

                 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

               id="WebApp_ID" version="3.0">

              

       

       

                 contextConfigLocation

                 classpath:beans.xml

       

       

                 org.springframework.web.context.ContextLoaderListener

       

       

       

       

                 springmvc

                 org.springframework.web.servlet.DispatcherServlet

                

                         contextConfigLocation

                         classpath:springmvc-servlet.xml

                

       

       

       

                 springmvc

                 *.action

       

         

       

       

       

                 SpringEncoding

                 org.springframework.web.filter.CharacterEncodingFilter

                

                         encoding

                         utf-8

                

       

       

                 SpringEncoding

                 /*

           

  1. 发布,测试

你可能感兴趣的:(MYBATIS)