目录
一、Mybatis与spring集成
1.导入pom依赖
2.创建spring配置文件applicationContext
3.注解式开发
二、Aop整合pagehelper插件
4、Spring Test+junit完美组合
配置文件
spring-hibernate.xml
注册数据库连接信息文件
配置数据库连接池
配置sessionfactory工厂信息-》hibernate.cfg.xml
配置hibernatetemplete
配置声明式事务
1.1 添加spring相关依赖(5.0.2.RELEASE)
spring-core
spring-beans
spring-context
spring-orm
spring-tx
spring-aspects
spring-web
1.2 添加mybatis相关依赖
mybatis核心:mybatis(3.4.5)
Mybatis分页:pagehelper(5.1.2)
1.4 添加dbcp2连接池
commons-dbcp2(2.1.1)
commons-pool2(2.4.3)
1.5 添加日志配置(2.9.1)
log4j-core
log4j-api
log4j-web
1.6 其他
junit(4.12)
javax.servlet-api(4.0.0)
lombok(1.18.2)注:使用mybatis-generator插件,pom文件添加支持
2.1 注解式开发
开启注解
2.2 引入外部jdbc配置文件
2.3 配置dbcp2数据库连接池
详见“dbcp2.txt”2.4 spring和mybatis整合
详见“spring与mybatis整合.txt”
ssh中数据的增删改查spring的配置如下
@Repository
@Service
@Autowired/@Resource:
@Constroller
@Autowired/@Resource:
spring ioc注入方式
set
构造
自动装配
bytype 根据属性对应的接口在spring的上下文匹配实现类
byname 根据属性名在spring上下文中寻找对应的id的Bean
hibernate与spring测试
applicationContext applicationContext = new classpathxmlappli..("appplicationContext.xml");
applicationContext。getBean("xxxservice");
@Repository:将DAO类声明为Bean
@Service:通常作用在业务层
@Constroller:通常作用在控制层,将在Spring MVC中使用
@Component:是一个泛化的概念,仅仅表示spring中的一个组件(Bean),可以作用在任何层次
@Scope:模式声明(singleton|prototype)
@Autowired:将自动在代码上下文与其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方
@Resource:
1)@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
2)指定了name或者type则根据指定的类型去匹配bean
3)指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错
helperDialect=mysql
package com.zking.ssm.biz;
import com.zking.ssm.model.Book;
import com.zking.ssm.model.BookVo;
import com.zking.ssm.util.PageBean;
import java.util.List;
import java.util.Map;
public interface BookBiz {
public int deleteByPrimaryKey(Integer bid);
public Book selectByPrimaryKey(Integer bid);
public List selectByIn(List bookIds);
public List selectBooksLike1(String bname);
public List selectBooksLike2(String bname);
public List selectBooksLike3(String bname);
List list1();
List list2();
List list3(BookVo vo);
List
package com.zking.ssm.biz.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zking.ssm.biz.BookBiz;
import com.zking.ssm.mapper.BookMapper;
import com.zking.ssm.model.Book;
import com.zking.ssm.model.BookVo;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class BookBizImpl implements BookBiz {
@Autowired
private BookMapper bookMapper;
// alt+insert 快速提供set/get/tostring/构造方法
// alt+enter 快速构建实现类,填充代码的前半部分 ctrl+1
// public BookMapper getBookMapper() {
// return bookMapper;
// }
// public void setBookMapper(BookMapper bookMapper) {
// this.bookMapper = bookMapper;
// }
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public List selectByIn(List bookIds) {
return bookMapper.selectByIn(bookIds);
}
@Override
public List selectBooksLike1(String bname) {
return bookMapper.selectBooksLike1(bname);
}
@Override
public List selectBooksLike2(String bname) {
return bookMapper.selectBooksLike2(bname);
}
@Override
public List selectBooksLike3(String bname) {
return bookMapper.selectBooksLike3(bname);
}
@Override
public List list1() {
return bookMapper.list1();
}
@Override
public List list2() {
return bookMapper.list2();
}
@Override
public List list3(BookVo vo) {
return bookMapper.list3(vo);
}
@Override
public List list4() {
return bookMapper.list4();
}
@Override
public Map list5(Map map) {
return bookMapper.list5(map);
}
@Override
public List listPager(Map map, PageBean pageBean) {
// pageHelper分页插件相关的代码
if(pageBean!=null&&pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
List maps = bookMapper.listPager(map);
if(pageBean!=null&&pageBean.isPagination()){
// 处理查询结果的前提是需要分页,是需要分页的
PageInfo info = new PageInfo(maps);
pageBean.setTotal(info.getTotal()+"");
}
return maps;
}
@Override
public List list6(BookVo bookVo) {
return bookMapper.list6(bookVo);
}
@Override
public List list7(BookVo bookVo) {
return bookMapper.list7(bookVo);
}
}
package com.zking.ssm.mapper;
import com.zking.ssm.model.Book;
import com.zking.ssm.model.BookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
// 通过in关键字进行查询:讲解foreach 标签的使用
// 如果说参数是非实体类(book,Order,....),那么急的加上注解 @param,bookIds是对应collection属性的
List selectByIn(@Param("bookIds") List bookIds);
List selectBooksLike1(@Param("bname") String bname);
List selectBooksLike2(@Param("bname") String bname);
List selectBooksLike3(@Param("bname") String bname);
// list1 list2的结论是,对于单表查询而言,可以用resultmap/resulttype接收,但是多表必须用resultmap接收
List list1();
List list2();
// 如果要传入多个查询参数,必须以对象的方式进行传递
// 举例:select * from t_mvc_book where bid in (1,2,3,4,5,6) and bname in ("圣墟","不死不休")
List list3(BookVo vo);
// 说明了不管返回1条数据,还是多条数据,都应该用java.util.Map进行接受
// 如果是1条数据,那么返回值是Map
// 如果是多条数据,那么返回值List
List list4();
Map list5(Map map);
// 利用第三方插件进行分页
List listPager(Map map);
/**
* 处理特殊字符
* @param bookVo
* @return
*/
List list6(BookVo bookVo);
/**
* 处理特殊字符
* @param bookVo
* @return
*/
List list7(BookVo bookVo);
}
package com.xnx.ssm;
import com.zking.ssm.biz.BookBiz;
import com.zking.ssm.biz.impl.BookBizImpl;
import com.zking.ssm.model.BookVo;
import com.zking.ssm.util.PageBean;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class BookBizImplTest2 {
@Autowired
private BookBiz bookBiz;
// private SqlSession sqlSession;
@Before
public void setUp() throws Exception {
System.out.println("初始化方法");
}
@After
public void tearDown() throws Exception {
System.out.println("方法测试结束");
}
@Test
public void deleteByPrimaryKey() {
bookBiz.deleteByPrimaryKey(44);
}
@Test
public void selectByPrimaryKey() {
System.out.println("测试的业务方法");
// System.out.println(bookBiz.getBookMapper());
System.out.println(bookBiz.selectByPrimaryKey(43));
}
@Test
public void test3() {
int[] ints={};
// 将数组变成字符串 1,2,3,4,5,6
StringBuffer sb=new StringBuffer();
for (int i:ints){
sb.append(",").append(i);
}
String s=sb.toString();
System.out.println(s.substring(1));
}
@Test
public void getBookMapper() {
}
@Test
public void setBookMapper() {
}
@Test
public void selectByIn() {
List bookIds = Arrays.asList(new Integer[]{31, 32, 33, 34});
bookBiz.selectByIn(bookIds).forEach(System.out::println);
}
@Test
public void selectBooksLike1() {
bookBiz.selectBooksLike1("%圣墟%").forEach(System.out::println);
}
@Test
public void selectBooksLike2() {
bookBiz.selectBooksLike2("%圣墟%").forEach(System.out::println);
}
@Test
public void selectBooksLike3() {
bookBiz.selectBooksLike3("圣墟").forEach(System.out::println);
}
@Test
public void list1() {
bookBiz.list1().forEach(System.out::println);
}
@Test
public void list2() {
bookBiz.list2().forEach(System.out::println);
}
@Test
public void list3() {
BookVo vo=new BookVo();
vo.setBookIds(Arrays.asList(new Integer[]{31,32,33,34}));
bookBiz.list3(vo).forEach(System.out::println);
}
@Test
public void list4() {
bookBiz.list4().forEach(System.out::println);
}
@Test
public void list5() {
Map map=new HashMap();
map.put("bid",32);
System.out.println(bookBiz.list5(map));
}
@Test
public void listPager() {
Map map=new HashMap();
map.put("bname","圣墟");
// bookBiz.listPager(map).forEach(System.out::println);
// 查询出第二页的20条数据
PageBean pageBean = new PageBean();
pageBean.setPage(2);
pageBean.setRows(20);
bookBiz.listPager(map,pageBean).forEach(System.out::println);
}
@Test
public void list6() {
BookVo vo=new BookVo();
vo.setMax(45);
vo.setMin(35);
bookBiz.list6(vo).forEach(System.out::println);
}
@Test
public void list7() {
BookVo vo=new BookVo();
vo.setMax(45);
vo.setMin(35);
bookBiz.list7(vo).forEach(System.out::println);
}
}