`
liyonghui160com
  • 浏览: 771553 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

MyBatis使用操作,springmvc

阅读更多

 

application.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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">

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:important.properties</value>
			</list>
		</property>
	</bean>
	
	<import resource="classpath*:datasource-mysql.xml" />
	
	
    <!-- enable component scanning (beware that this does not enable mapper scanning!) -->    
    <context:component-scan base-package="com.xx.xxdp.service" />
    <context:component-scan base-package="com.xx.xxdp.web.action" />

    <!-- enable autowire -->
    <context:annotation-config />

    <!-- enable transaction demarcation with annotations -->
    <tx:annotation-driven />
    
   
</beans>
 

 

 

 

   mybatis的配置文件:

 

 datasource-mysql.xml

 

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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">

	<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="${db.mysql.url}?characterEncoding=utf-8&amp;connectTimeout=1000&amp;autoReconnect=true"></property>
		<property name="username" value="${mysql.username}"></property>
		<property name="password" value="${mysql.password}"></property>
		<property name="initialSize" value="1" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="20" />
		<property name="maxWait" value="1000" />
		<property name="poolPreparedStatements" value="true"/>
		<property name="validationQuery" value="select 1" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="true" />
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>
    <bean id="mysqlTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="mysqlDataSource"/>  
    </bean>
    <tx:annotation-driven transaction-manager="mysqlTransactionManager"/>


    <!-- mybatis -->
    <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="mysqlDataSource" />
        <property name="typeAliasesPackage" value="com.xx.xxdp.domain.mysql" />
        <property name="mapperLocations" value="classpath*:sqlmap/mysql/*.xml" />
    </bean>
    <!--inject dao list -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory"></property>  
        <property name="basePackage" value="com.xx.xxdp.dao.mysql" />
    </bean>
    
</beans>

 

 

 DemoMysql2DaoMapper.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="com.xx.xxdp.dao.mysql.demo.DemoMysql2Dao">
	<insert id="insert" parameterType="DemoMysql2" >
		insert into demoMysql2  values (#{id}, #{name})
	</insert>

	<update id="update" parameterType="DemoMysql2" >
		update demoMysql2
		<set>
			<if test="name != null" >
				name = #{name}
			</if>
		</set>

		where id = #{id}
	</update>

	<delete id="delete" >
		delete from demoMysql2 where id = #{id}
	</delete>
	
	<select id="get"  resultType="DemoMysql2">
	    select name from demoMysql2 where id = #{id}
	</select>
	
	<select id="list"  resultType="DemoMysql2">
	    select name from demoMysql2
	</select>
	
</mapper>

 

 

DemoMysql2Dao.java

 

 

package com.xx.xxdp.dao.mysql.demo;

import java.util.List;

import com.xx.xxdp.domain.mysql.demo.DemoMysql;
import org.apache.ibatis.annotations.Param;

import com.xx.xxdp.domain.mysql.demo.DemoMysql2;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


/**
 * demo dao
 */
@Repository
public interface DemoMysql2Dao {
	public int insert(DemoMysql2 demo);//注意方法名和Mapper中定义一致
	public int delete(@Param("id") Integer id);
	public int update(DemoMysql2 demo);
	public DemoMysql2 get(@Param("id") Integer id);
	public List<DemoMysql2> list();
    //注解形式
    @Select("select name,title from demoMysql2")
    public List<DemoMysql2> getList();
}

 

service接口

 

package com.xx.xxdp.service.demo;

import org.springframework.transaction.annotation.Transactional;

import com.xx.xxdp.domain.hivemeta.demo.DemoHiveMeta;
import com.xx.xxdp.domain.mysql.demo.DemoMysql;

import java.util.List;



public interface DemoService {

	/**
	 * 跨数据源的事务, DemoMysql和DemoHiveMeta位于不同的数据库
	 * @param demoMysql
	 * @param meta
	 */
	@Transactional
	public void update(DemoMysql demoMysql, DemoHiveMeta meta);
        //注解的形式
        public List<DemoMysql> setList();
}

 

   Impl调用实例

 

package com.xx.xxdp.service.demo.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xx.xxdp.dao.hivemeta.demo.DemoHiveMetaDao;
import com.xx.xxdp.dao.mysql.demo.DemoMysqlDao;
import com.xx.xxdp.domain.hivemeta.demo.DemoHiveMeta;
import com.xx.xxdp.domain.mysql.demo.DemoMysql;
import com.xx.xxdp.service.demo.DemoService;

import java.util.List;


/**
 * demo服务实现类
 * @author  
 */
 @Service("demoService")
public class DemoServiceImpl implements DemoService {
	private final static Logger LOG = Logger.getLogger(DemoServiceImpl.class);
	@Autowired
	private DemoMysqlDao demoMysqlDao;
	@Autowired
	private DemoHiveMetaDao demoHiveMetaDao;
	
	public void update(DemoMysql demoMysql, DemoHiveMeta meta) {

		demoMysqlDao.update(demoMysql);
		if (  1 == 1 ) {
			throw new RuntimeException();
		}
		demoHiveMetaDao.update(meta);
	}

    public List<DemoMysql> setList(){
        return demoMysqlDao.getList();
    }
}

 

 

 接下来就是@Controller调用了

 

@Controller
public class SingleAnalysisControl {
    private static final Logger log = Logger.getLogger(Control.class);
    @Resource(name="demoService")
    private demoServiceDemoService;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public ModelAndView getSingleAnalysis(HttpServletRequest httpServletRequest,@RequestParam(value = "sku", required = false, defaultValue = "") String sku,@RequestParam(value = "theDay", required = false, defaultValue = "") String theDay) {
        ModelAndView view = new ModelAndView();
        List<DemoMysql> listPro=null;
        try {
            if (theDay.isEmpty()) {
                return null;
            } else { 
                listPro = demoService.getList();
                view.addObject("listViewsSourceDetail",listPro);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return view;
    }
}

 

分享到:
评论

相关推荐

    spring4+mybatis3+springmvc4实战

    总的来说,《Spring4+Mybatis3+SpringMVC4实战》项目是一个全面的教程,涵盖了从基础到高级的SSM框架应用,通过实际操作,你不仅可以掌握SSM的配置和使用,还能提高解决实际问题的能力,为成为一名合格的Java Web...

    SpringMVC+Mybatis demo

    总之,"SpringMVC+Mybatis demo"展示了如何使用这两个框架协作构建一个完整的Web应用。SpringMVC处理请求和响应,MyBatis负责数据库操作,Service层作为它们之间的桥梁,实现了业务逻辑。这样的组合提供了良好的分层...

    Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合)

    Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合) Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合) Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合)

    springMvc+spring+Mybatis 实战案例 实用!

    在Spring中,MyBatis可以与Spring无缝集成,通过SqlSessionFactory和SqlSession对象,开发者可以方便地执行增删查改操作,同时,MyBatis的Mapper接口和XML配置文件或者注解方式让SQL与代码分离,提高了代码的可读性...

    spring+mybatis+springmvc 全注解框架

    在这个框架中,Spring会管理Bean的生命周期和依赖关系,MyBatis通过Mapper接口与XML映射文件进行SQL操作,SpringMVC则接收并处理HTTP请求,调用相应的业务服务。这种集成方式简化了开发流程,提高了开发效率,使得...

    mybatis 和springmvc整合

    在IT行业中,MyBatis和SpringMVC是两个非常重要的框架,它们分别专注于持久层操作和Web MVC(Model-View-Controller)架构。本文将详细阐述如何将MyBatis与SpringMVC进行整合,以便在实际开发中实现高效、灵活的数据...

    mybatis generator 代码生成 springmvc可用

    然后,你就可以在Controller层通过@Autowired注入DAO,直接使用MBG生成的数据库操作方法。 总结来说,MyBatis Generator是一个强大的代码生成工具,它可以极大地提高开发效率,特别是在SpringMVC项目中。通过正确...

    spring4.0+mybatis3.2+springMvc+easyui1.3.2

    《整合Spring4.0、MyBatis3.2、SpringMVC与EasyUI1.3.2:构建兼容IE8的权限管理系统》 在Web应用开发中,选用合适的框架组合可以大大提高开发效率并优化系统架构。"spring4.0+mybatis3.2+springMvc+easyui1.3.2"是...

    如何将mybatis配置到springmvc中

    在开发Java Web应用程序时,整合MyBatis与SpringMVC框架可以使数据访问...这使得开发者可以利用SpringMVC的强大功能处理HTTP请求,同时享受MyBatis提供的简洁数据库操作。整个过程大大提升了开发效率和代码的可维护性。

    Java项目框架-SpringMVC+Spring+Mybatis集成开发环境

    SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+...

    Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统.rar

    项目描述 在上家公司自己集成的一套系统,用了两个多月的时间完成的:Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级开发系统 Springboot作为容器,使用mybatis作为持久层框架 使用官方推荐的thymeleaf做为...

    mybatis3+springmvc4+angularjs+mysql+decorator实例

    这是一个整合了多个技术框架的项目实例,主要涵盖了MyBatis3、SpringMVC4、AngularJS、MySQL数据库以及Decorator模式的应用。以下是对这些技术及其在项目中的应用进行的详细解释: 1. MyBatis3:MyBatis是一个优秀...

    mybatis+springmvc+spring整合jar包

    SSM(Spring、SpringMVC、MyBatis)是一个经典的Java web开发框架组合,广泛应用于企业级应用开发中。这个整合的jar包包含了这三个组件的核心库,使得开发者能够快速搭建基于这些技术的项目结构。 首先,Spring框架...

    mybatis整合springMvc完整jar包

    MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。...通过以上整合,开发者可以在SpringMVC的Web环境中轻松地使用MyBatis进行数据库操作,提高开发效率,同时享受到Spring的诸多优点。

    Mybatis3+SpringMVC+Spring+ 注解+Maven DEMO

    【标题】"Mybatis3+SpringMVC+Spring+ 注解+Maven DEMO" 涉及到的是一个常见的Java Web开发框架集成示例,这个DEMO旨在展示如何将Mybatis3、SpringMVC、Spring框架以及Maven构建工具整合在一起,以实现高效、模块化...

    SpringMVC+Redis+MyBatis项目

    在本项目中,我们探索了如何整合SpringMVC、Redis和MyBatis三大技术来构建一个高效、可扩展的Web应用程序。SpringMVC是Spring框架的一部分,主要用于处理HTTP请求和响应,提供MVC(Model-View-Controller)设计模式...

    整合mybatis mybatisplus spring springmvc

    在IT行业中,SSM框架(Spring、SpringMVC、MyBatis)是Java Web开发中的经典组合,而MyBatisPlus则是在MyBatis基础上提供更便捷操作数据库的工具。本篇文章将深入探讨如何整合这四个组件,以及它们各自的核心功能。 ...

    mybatis+springmvc整合

    在IT行业中,MyBatis和SpringMVC是两个非常重要的框架,它们分别专注于持久层和Web层的开发。本文将详细介绍如何将MyBatis与SpringMVC整合,以实现一个完整的增删改查(CRUD)应用,并涉及所需的库文件。 MyBatis是...

    springmvc+mybatis+redis

    总结来说,"springmvc+mybatis+redis"的整合是现代Web开发中常见的技术栈组合,它利用SpringMVC提供灵活的控制层,MyBatis简化数据库操作,而Redis则通过高效的缓存机制优化了数据访问性能。这种组合能够帮助开发者...

    SSM.zip_mybatis_spring springmvc _ssm功能实现_ssm框架实站

    SSM框架的整合,使得开发者可以充分利用Spring的管理能力,SpringMVC的Web处理能力,以及MyBatis的灵活数据库操作。这种集成可以实现以下功能: 1. **依赖注入**:Spring通过DI将对象之间的依赖关系解耦,使代码更...

Global site tag (gtag.js) - Google Analytics