SSM框架集成.上(供自己练习查阅用)
Posted zhangxiong-tianxiadiyi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架集成.上(供自己练习查阅用)相关的知识,希望对你有一定的参考价值。
1.创建项目
2.准备相关依赖包
2.1 spring相关jar包
2.2 springMVC相关jar包
2.3 mybatis 相关jar包
2.4mybatis 和spring集成的桥梁包
2.5数据库驱动包和连接池
2.6 jstl 标签库依赖包
2.7 mybatis支持的日志包
3.配置文件准备
4.mapper层(dao层代码)
public interface UserMapper int insert(User user); User selectByPrimaryKey(Integer id); List<User> selectList(); int delteByPrimaryKey(Integer id);
mapper.xml 配置文件代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.zj.ssm.mapper.UserMapper"> <insert id="insert" parameterType="cn.zj.ssm.pojo.User"> insert into user (name,password,age)values(#name,#password,#age) </insert> <select id="selectByPrimaryKey" parameterType="Integer" resultType="cn.zj.ssm.pojo.User"> select * from user where id = #id </select> <select id="selectList" resultType="cn.zj.ssm.pojo.User"> select * from user </select> <delete id="delteByPrimaryKey" parameterType="int"> delete from user where id = #id </delete> </mapper>
5.service层代码
@Service public class UserServiceImpl implements UserService /* * 问题: UserMapper 代理对象如何创建? * 答 :使用 SqlSession 操作对象创建 ! * * 问题 : SqlSession 对象如何创建? * * 答 : SqlSessionFactory 工厂对象创建? * * 问题: SqlSessionFactory 对象如何创建 * 1,和Spring框架集成之前 * MyBatis框架自己读取配置文件中的相关配置去创建 * 2, 和Spring框架集成之后 * 交个Spring容器来创建 * 问题: 如何在Spring框架中配置,创建出来SqlSessionFactory对象? * mybatis和spring集成的类查阅 桥梁包 * org.mybatis.spring.SqlSessionFactoryBean 创建 SqlSessionFactory * */ @Autowired private UserMapper mapper; @Override public int insert(User user) return mapper.insert(user); @Override public User selectByPrimaryKey(Integer id) System.out.println(mapper); return mapper.selectByPrimaryKey(id); @Override public List<User> selectList() return mapper.selectList(); @Override public int delteByPrimaryKey(Integer id) return mapper.delteByPrimaryKey(id);
6.测试代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring.xml") public class UserServiceTest @Autowired private UserService service; @Test public void testInsert() User user = new User(null, "乔峰", "qiaofeng", 30); int row = service.insert(user); System.out.println(row); @Test public void testSelectByPrimaryKey() User user = service.selectByPrimaryKey(8); System.out.println(user); @Test public void testSelectList() throws Exception List<User> users = service.selectList(); for (User user : users) System.out.println(user);
7.applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 设置注解配置包扫描位置 --> <context:component-scan base-package="cn.zj.mybatis"/> </beans>
以上是关于SSM框架集成.上(供自己练习查阅用)的主要内容,如果未能解决你的问题,请参考以下文章
Spring+SpringMvc+Mybatis框架集成搭建教程