SM框架(总结整合Spring和MyBatis框架)

SM框架(总结整合Spring和MyBatis框架),第1张

SM框架(总结整合Spring和MyBatis框架)

导入spring和MyBatis使用的jar包

 

 mybatis-spring-1.2.0.jar
下载路径:http://github.com/mybatis/spring/releases

mybatis-spring-1.3.2.jar 下载路径:
 

添加jdbc配置文件

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=utf8

jdbc.username=epetadmin

jdbc.password=root

添加MyBatis-config xml配置文件





    
        
    
    
        
    
    

添加spring-config xml文件 





    
    
    
    
        
        
        
        
    

    

        

        
    

    

        
        
    


    
        
    

将MyBatis的核心组件由spring中IOC接管创建并进行依赖管理。

  • 构建SqlSessionFactory

创建实体类

public class Pet {

    private int id;
    private int masterId;
    private String name;
    private int typeId;
    private int health;
    private int love;
    private Timestamp adopt_time;
    private int status;
    //get set ………………
}

创建dao层及接口对应的mapper xml文件

public interface EpetDao {
    
    List queryEpets();

}




    
        SELECT * FROM pet ;
    

创建实现类

@Repository("epetDaoImpl")
public class EpetDaoImpl implements EpetDao{
    @Resource(name = "sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    
    @Override
    public List queryEpets() {

        return  sqlSessionTemplate.getMapper(EpetDao.class).queryEpets();
    }
}

创建service层

@Service("epetServiceImpl")
public class EpetServiceImpl implements EpetService{
    @Resource(name = "epetDaoImpl")
    private EpetDao epetDao;

    @Override
    public List getList() {
        return epetDao.queryEpets();
    }

    public EpetDao getEpetDao() {
        return epetDao;
    }

    public void setEpetDao(EpetDao epetDao) {
        this.epetDao = epetDao;
    }

}

创建controller层

public class PetControl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        EpetServiceImpl ser = context.getBean("epetServiceImpl", EpetServiceImpl.class);
        List list = ser.getList();
    }
}

测试

public class TestPet {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        EpetServiceImpl service = context.getBean("epetServiceImpl", EpetServiceImpl.class);
        List list = service.getList();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

    }
}

测试结果

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e268090] was not registered for synchronization because synchronization is not active
JDBC Connection [jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=utf8, UserName=epetadmin@localhost, MySQL Connector Java] will not be managed by Spring
==>  Preparing: SELECT * FROM pet ;
==> Parameters: 
<==    Columns: id, master_id, name, type_id, health, love, adopt_time, status
<==        Row: 3, 1, qw, 1, 99, 55, 2021-02-02 00:00:00.0, 0
<==        Row: 4, 1, er, 1, 99, 55, 2021-02-02 00:00:00.0, 1
<==        Row: 5, 1, yt, 1, 99, 55, 2021-02-02 00:00:00.0, 1
<==        Row: 6, 1, wf, 1, 99, 55, 2021-02-02 00:00:00.0, 1
<==      Total: 4
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e268090]
com.bdqn.pojos.Pet@428640fa
com.bdqn.pojos.Pet@d9345cd
com.bdqn.pojos.Pet@2d710f1a
com.bdqn.pojos.Pet@29215f06

Process finished with exit code 0

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4658279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存