配置环境升级jdk1.6升级为jdk1.7;(log4j-1.2.17.jar)
持久层框架
特点:半自动ORM映射,将数据库中的数据封装到对象中
SqlSessionFactory 线程安全性
SqlSession 线程非安全,不能做类的公用变量
4)Mybatis核心配置文件 sqlMapConfig.xml
配置内容:
5)Mapper文件
A.对一association javaType
B.对多 collection ofType
jdbcType
它是指定当NAME为null时,给jdbc驱动程序告诉它,它针对的数据库字段的类型。(Oracle)
update person
where id = #{id}
6)Mapper中写SQL
获取参数
#{} 它会自动根据参数类型做封装。例如对字符串类型,两边加单撇;对整数类型直接使用;好处防止SQL注入(推荐)
${} 将用户填入的参数直接拼接到SQL。(字符串直接拼接)坏处:SQL注入;例如:拼接order by条件(特殊的地方)
7)SQL中含有特殊字符
8)动态SQL
9)常规的标签
10)参数:
11)返回值:
SQL返回的结果集中字段不能有重名的,如果有,必须设置别名,通过resultMap映射时进行转化。
12)类中调用mybatis
sqlSession提供很多方法
selectList 查询多条,返回结果集封装到List
selectOne 查询一条,必须返回一个值,如果不是,报错;
insert 新增
update 修改
delete 删除
13)两种调用形式:
使用sqlsession提供selectList…,它们调用方式不习惯,它命名空间.id,如果字母写错,在编译时无法提示。运行报错。
通过命名空间找的mapper文件,在通过id去找到一个具体sql去执行。
package cn.itcast.mapper;
import java.util.List;
import java.util.Map;
import cn.itcast.domain.Person;
/**
* @Description:
* @Author: 传智播客 java学院 陈子枢
* @Company: http://java.itcast.cn
* @CreateDate: 2014年11月17日
*/
public interface PersonMapper {
public List
public Person get(Integer id);
public void insert(Person person);
public void update(Person person);
public void deleteById(Integer id);
public void deleteList(List list);
public void deleteMap(Map map);
public Integer count();
}
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import cn.itcast.domain.Person;
import cn.itcast.mapper.PersonMapper;
/**
* @Description:
* @Author: 传智播客 java学院 陈子枢
* @Company: http://java.itcast.cn
* @CreateDate: 2014年11月17日
*/
public class TestMapperInterface {
private SqlSessionFactory factory;
@Before //最先执行,初始化SessionFactory
public void init() throws IOException{
String resource = "sqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(is);
}
@Test
public void testFind(){
SqlSession session = factory.openSession();
//获取接口对象
PersonMapper mapper = session.getMapper(PersonMapper.class);
Map map = new HashMap();
map.put("name", "'t%'");
List
System.out.println(pList.size());
}
@Test
public void testGet(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person p = mapper.get(1);
System.out.println(p.getName());
}
@Test
public void testInsert(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person p = new Person();
p.setId(4);
p.setName("tom");
p.setAge(8);
mapper.insert(p);
session.commit();
}
@Test
public void testUpdate(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person p = mapper.get(4);
p.setAge(9);
mapper.update(p);
session.commit();
}
@Test
public void testDeleteById(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
mapper.deleteById(4);
session.commit();
}
@Test
public void testDeleteList(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
List
list.add(5);
list.add(6);
mapper.deleteList(list);
session.commit();
}
@Test
public void testDeleteMap(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Map map = new HashMap();
map.put("ids", new Integer[]{4,5,6,7});
mapper.deleteMap(map);
session.commit();
}
@Test
public void testCount(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
int i = mapper.count();
System.out.println(i);
}
}
可以随意放在任何一个工程中,它可以通过配置指向要生成的工程
在配置文件中自己指定当前环境中的驱动jar
工具如果再次生成会覆盖之前的内容
大量减少写代码的工作量,代码比较规范。
日常就拷贝insert,update,delete和字段;实体
关联关系它不会生成,手工修改;
项目组的规范不同,手工修改;
生成的example内容都要手工清除
一级缓存Hibernate,session级别
二级缓存Hibernate,sessionFactory级别
Mybatis它和hibernate一样
@Test //一级缓存
public void cacheLevel1(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Map map = new HashMap();
map.put("name", "'tony'");
List
System.out.println(personList.size());
PersonMapper mapper2 = session.getMapper(PersonMapper.class);
List
System.out.println(personList2.size());
}
默认Mybatis二级缓存不开启,
如何开启
使用
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
@Test //二级缓存
public void cacheLevel2(){
SqlSession session = factory.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Map map = new HashMap();
map.put("name", "'tony'");
List
System.out.println(personList.size());
session.close(); //关闭session
session = factory.openSession();
PersonMapper mapper2 = session.getMapper(PersonMapper.class);
List
System.out.println(personList2.size());
}
参数配置
在实际开发中,经过在线上压力测试,找到一个符合当前客户运行环境下最恰当的值。
默认mybatis二级缓存一般
第三方cache中有两个佼佼者;
Ehcache 单机
Momeycache 集群
修改为ehcache.xml即可
拦截器interceptor
Mybatis 分页
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
Springmvc 性能监控
开发步骤:
1、编写拦截器,记录Controller方法执行时间
package cn.itcast.jk.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.NamedThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* @Description:
* @Author: nutony
* @Company: http://java.itcast.cn
* @CreateDate: 2014-3-6
*/
public class TimerInterceptor implements HandlerInterceptor {
private NamedThreadLocal
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
long beginTime = System.currentTimeMillis(); //开始时间
startTimeThreadLocal.set(beginTime);
return true;
}
public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {
long endTime = System.currentTimeMillis();
long executeTime = endTime - startTimeThreadLocal.get();
System.out.println(String.format("%s execute %d ms." , req.getRequestURI() , executeTime));
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
}
}
2、在springmvc-servlet.xml文件中配置拦截器
对多个controller进行指定方法的拦截
对多个controller的所有方法拦截
对某目录下的controller进行拦截
拦截整个项目的所有controller
Springmvc+spring+hibernate
Springmvc+spring+mybatis
开发步骤:
aopalliance-1.0.jar
asm-3.3.1.jar
cglib-2.2.2.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
ehcache-core-2.6.5.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
junit-4.9.jar
log4j-1.2.16.jar
mybatis-3.2.2.jar
mybatis-ehcache-1.0.2.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.10-bin.jar
ognl-3.0.6.jar
slf4j-api-1.6.2.jar
slf4j-log4j12-1.6.2.jar
spring-aop-3.2.2.jar
spring-beans-3.2.2.jar
spring-context-3.2.2.jar
spring-core-3.2.2.jar
spring-expression-3.2.2.jar
spring-jdbc-3.2.2.jar
spring-orm-3.2.2.jar
spring-tx-3.2.2.jar
spring-web-3.2.2.jar
spring-webmvc-3.2.2.jar
package cn.itcast.ssm.domain;
import java.io.Serializable;
import java.util.List;
/**
* @Description:
* @Author: 传智播客 java学院 陈子枢
* @Company: http://java.itcast.cn
* @CreateDate: 2014年11月15日
*/
public class Person implements Serializable {
private Integer id;
private String name;
private Integer age;
private String remark;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from person
where 1=1
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
}
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
}
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
PersonMapper mapper = this.getSqlSession().getMapper(PersonMapper.class);
return mapper.find(map);
}
}
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
}
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
return personDao.find(map);
}
}
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
model.addAttribute("personList", personList); //传递到页面
return "/person/jPersonList.jsp";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
人员列表
序号 | 姓名 | 年龄 | 备注 |
${status.index+1} | ${p.name} | ${p.age} | ${p.remark} |
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
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">
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 ">
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">