SSM框架集成.上(供本身練習查閱用)

1.建立項目spring

2.準備相關依賴包數據庫

  2.1   spring相關jar包mybatis

        

   2.2   springMVC相關jar包app

         

   2.3 mybatis 相關jar包框架

       

    2.4mybatis 和spring集成的橋樑包ide

  

    2.5數據庫驅動包和鏈接池測試

   

   

    2.6 jstl 標籤庫依賴包spa

   

     2.7 mybatis支持的日誌包3d

  

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>
相關文章
相關標籤/搜索