精华内容
下载资源
问答
  • SpringMVC Spring MyBatis整合

    万人学习 2015-05-15 14:35:20
    1.spring数据源配置 2.SpringMVC Spring MyBatis整合 3.SpringMVC Spring MyBatis实现单表增、删、改、查
  • Spring mybatis 整合

    热门讨论 2013-06-21 16:14:46
    经过测试,整合spring3.2.3和mybatis3.2.2。 内含spring-3.2.3的必备jar包,包含sqlserver2008的连接jar包 包含struts2的jar包(但没有进行整合),含有mybatis-3.2.2和mybatis-spring-1.2.0的jar包
  • Spring Mybatis 整合

    千次阅读 2013-07-17 16:12:51
    1.所需要的包: 1,spring发行包 2,mybatis 包和Mybatis-Spring 的jar包,maven可以使用: org.mybatis mybatis-spring 1.2.0 compile org.mybatis mybatis 3.2.1 comp

    1.所需要的包:

    1,spring发行包

    2,mybatis 包和Mybatis-Spring 的jar包,maven可以使用:

    <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.0</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.2.1</version>
          <scope>compile</scope>
        </dependency>

    2.配置

    spring applicationContent.xml 文件主要内容:

    <!-- MyBatis配置 -->
    	
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ---------1
    		<property name="dataSource" ref="dataSource" /> -------2
    		<!-- 显式指定Mapper文件位置 -->
    		<property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml" />  -------3
    		<property name="typeAliasesPackage" value="com.blueinfo.jee.entity" />      -------4
    	</bean>
    	<!-- scan for mappers and let them be autowired -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">	------5
    		<property name="basePackage" value="com.blueinfo.jee.reposity.mybatis" />  --------6		
    		<property name="annotationClass" value="com.blueinfo.jee.reposity.mybatis.MyBatisReposity"/> --------7
    	</bean>

    注释:

    1,定义SqlSessionFactoryBean,在这个bean里面还是通过SqlSessionFactoryBuilder来建立对应的SqlSessionFactory,进而获取到对应的SqlSession。通过SqlSessionFactoryBean我们可以通过对其指定一些属性来提供Mybatis的一些配置信息。

    2,datasource定义,不能缺少

    3,显示指定Mapper.xml的位置

    4,指定实体类的包位置

    5,通过配置MapperScannerConfigurer,Mybatis-Spring会自动为我们注册Mapper对应的MapperFactoryBean对象,而不用使用类似下面的配置一个个指定

    <bean id="myMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
          <property name="mapperInterface"  
              value="com.blueinfo.jee.BlogMapper" />  
          <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
       </bean>
    上述方式,有一个Mapper就要定义一个bean。

    6,指定接口的包,MapperScannerConfigurer会扫面包下面的所有Mapper接口。并把它们注册为一个个MapperFactoryBean对象

    7,通过注解过滤基本包下面的接口。使得MapperScannerConfigurer只扫描basePackage下面带注解MyBatisReposity的接口

    有时候我们指定的基包下面的并不全是我们定义的Mapper接口,为此MapperScannerConfigurer还为我们提供了另外两个可以缩小搜索和注册范围的属性。一个是annotationClass,另一个是markerInterface。

    • annotationClass:当指定了annotationClass的时候,MapperScannerConfigurer将只注册使用了annotationClass注解标记的接口。

    • markerInterface:markerInterface是用于指定一个接口的,当指定了markerInterface之后,MapperScannerConfigurer将只注册继承自markerInterface的接口。 如果上述两个属性都指定了的话,那么MapperScannerConfigurer将取它们的并集,而不是交集。即使用了annotationClass进行标记或者继承自markerInterface的接口都将被注册为一个MapperFactoryBean。


    3.源文件


    实体类:

    @Entity
    @Table(name = "ss_user")
    public class User extends IdEntity {
    
    	private String loginName;
    	private String plainPassword;
    	private String password;
    	private String salt;
    	private String name;
    	private String email;
    	private String status;
    
    	//.....
    
    	
    
    }

    @MappedSuperclass
    public abstract class IdEntity {
    
    	protected Long id;
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	public Long getId() {
    		return id;
    	}
    
    	public void setId(Long id) {
    		this.id = id;
    	}
    }


    注解MyBatisReposity


    /**
     * 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。
     * 
     * @author calvin
     *
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @Documented
    @Component
    public @interface MyBatisReposity {
    	String value() default "";
    }
    

    dao:

    @MyBatisReposity
    public interface UserMybatisDao {
    	
    	User get(Long id);
    
    	List<User> search(Map<String, Object> parameters);
    
    	void save(User user);
    
    	void delete(Long id);
    
    }


    service:

    @Service
    public class AccountService implements IAccoutService{
    	
    	@Autowired
    	private UserMybatisDao userMbDao;
    	
    	
    	
    	public void getUser(long id){
    		User user = userMbDao.get(id);
    		logger.info("user name:" + user.getLoginName());
    	}
    	
    	Logger logger = LoggerFactory.getLogger(AccountService.class);
    
    }

    UserMapper.xml 与dao接口一一对应

    <?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">
    <!-- namespace必须指向Dao接口 -->
    <mapper namespace="com.blueinfo.jee.reposity.mybatis.UserMybatisDao">
    	<!--
    		获取用户: 输出直接映射到对象, login_name列要"as loginName"以方便映射 ,team_id as "team.id"创建team对象并赋值
    	-->
    	<select id="get" parameterType="long" resultType="User">
    		select id, name, email,
    		login_name as loginName,
    		team_id as "team.id"
    		from ss_user
    		where id=#{id}
    	</select>
    	
    </mapper> 
    

    test类

    @RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration(locations = {"classpath:/applicationContext.xml"}) 
    @Transactional 
    public class AccountServiceTest {
    	
    	@Autowired
    	private AccountService service;
    	
    	@Test
    	public void testGetUser() {
    		
    		User user = service.getUser(1L);
    		
    		assertNotNull("User not found", user);
    		assertEquals("test", user.getLoginName());
    	}
    
    }
    

    文章内容参考开源项目SpringSide,地址:https://github.com/springside


    展开全文
  • 1.spring数据源配置 2.SpringMVC Spring MyBatis整合 3.SpringMVC Spring MyBatis实现单表增、删、改、查
    SpringMVC Spring MyBatis整合—31646人已学习 
    课程介绍    
    201505150634257046.gif
        1.spring数据源配置 2.SpringMVC Spring MyBatis整合 3.SpringMVC Spring MyBatis实现单表增、删、改、查
    课程收益
        熟练使用SpringMVC Spring MyBatis开发项目
    讲师介绍
        刘英杰更多讲师课程
        Java讲师,多年培训经验,曾参与开发过的项目:某省电信ODS系统、电信自助取数系统、CRM客户关系管理系统、环境应急信息系统、自来水客服系统等。熟练掌握J2EE平台开发技能,包括Core Java、Servlet、SSH、JSP、EJB、JNDI、JDBC、JTS等技术
    课程大纲
        1.springmvc spring mybatis整合一  22:32
        2.springmvc spring mybatis整合二  22:03
        3.springmvc spring mybatis整合三  16:30
        4.springmvc spring mybatis整合四  17:36
        5.springmvc spring mybatis整合五  17:53
    大家可以点击【查看详情】查看我的课程
    展开全文
  • <artifactId>aspectjweaverartifactId> <version>1.9.5version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>2.0.4version> dependency> <dependency> ...
     <dependencies>
    <!--        测试依赖-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    <!--        mysql数据库依赖,mybatis需要的依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
    <!--        mybatis依赖-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <!--这个是spring核心依赖 ioc-di以及配置方式实现aop -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
            <!-- Resgource依赖-->
            <dependency>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
            </dependency>
    
            <!-- 注解实现aop依赖-->
            <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
            </dependency>
    <!--        spring,mybatis整合所需核心依赖-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.4</version>
            </dependency>
    <!--        spring操作数据库所需要的依赖,spring,mybatis整合所需依赖-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
        </dependencies>
    
    
    展开全文
  • Spring Mybatis整合小例子

    千次阅读 2018-08-09 14:33:38
    1、新建JavaProject工程 spring-mybatis、部署spring、新建包、添加properties属性文件。目录如下: 2、applicationConetxt.xml文件已经把mybatis-config.xml中的dataSource、mappers等配置整合成bean或peroperty...

    1、新建JavaProject工程 spring-mybatis、部署spring、新建包、添加properties属性文件。目录如下:

    2、applicationConetxt.xml文件已经把mybatis-config.xml中的dataSource、mappers等配置整合成bean或peroperty

    <?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:p="http://www.springframework.org/schema/p"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
        http://www.springframework.org/schema/mvc    
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      
      <!-- 自动扫描所有的包,加载装配所有的实体类bean -->
      <context:component-scan base-package="com"/>
     
     <!-- 加载配置文件 -->
     <context:property-placeholder location="classpath:*.properties" />
     
     <!-- 配置数据源 -->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${driver}"/>
      <property name="url" value="${url}"/>
      <property name="username" value="${user}"/>
      <property name="password" value="${password}"/>
     </bean>
     
     <!-- sqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- 加载 mapper.xml的配置文件 -->
      <property name="mapperLocations" value="com/mapper/*.xml"/>
      <!-- 添加所需的数据源 -->
      <property name="dataSource" ref="dataSource"/>
     </bean>
     
     <!-- 扫描Dao包,加载接口映射类 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.mapper" />
     	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 	
     </bean>
     
     
     <!-- (事务管理)transaction manager -->  
        <bean id="transactionManager"  
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
            <property name="dataSource" ref="dataSource" />  
        </bean> 
     
     
    </beans>

    3、mapper文件

     package com.mapper;
    import java.util.List;
    import com.model.Student;
    
    public interface StudentMapper {
    
    	public List<Student> getAllStudent();
    }
    
    <?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.mapper.StudentMapper">
    
    	<select id="getAllStudent" resultType="com.model.Student">
    		select * from student
    	</select>
    
    </mapper>
    

    4、实体类Student

    package com.model;
    
    public class Student {
    	private int sno;
    	private String sname;
    	private double score;
    	
    	public int getSno() {
    		return sno;
    	}
    	public void setSno(int sno) {
    		this.sno = sno;
    	}
    	public String getSname() {
    		return sname;
    	}
    	public void setSname(String sname) {
    		this.sname = sname;
    	}
    	public double getScore() {
    		return score;
    	}
    	public void setScore(double score) {
    		this.score = score;
    	}
    	public Student(int sno, String sname, double score) {
    		super();
    		this.sno = sno;
    		this.sname = sname;
    		this.score = score;
    	}
    	public Student() {
    		super();
    	}
    	@Override
    	public String toString() {
    		return "Student [sno=" + sno + ", sname=" + sname + ", score=" + score + "]";
    	}
    	
    	
    }
    

    5、从配置文件中获得ApplicationContext上下文的工具类

    package com.tool;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AppContext {
    
    	private static ApplicationContext appContext;
    	
    	static{
    		appContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    	}
    	
    	public static <T>T getBean(Class<T> t){
    		return appContext.getBean(t);
    	}
    }
    

    6、service业务包

    package com.service;
    
    import java.util.List;
    
    import com.model.Student;
    
    public interface InterStudentService {
    
    	public List<Student> showStudentList();
    }
    
    package com.service;
    
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    
    import com.mapper.StudentMapper;
    import com.model.Student;
    import com.tool.AppContext;
    
    @Service
    public class StudentServiceImpl implements InterStudentService {
    
    	@Resource
    	private StudentMapper studentMapper;	
    
    	@Override
    	public List<Student> showStudentList() {
    		
    		//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		
    		studentMapper=AppContext.getBean(StudentMapper.class);
    		
    		return studentMapper.getAllStudent();
    	}
    
    }
    

    7、测试类Test

    package com.test;
    
    import com.service.InterStudentService;
    import com.service.StudentServiceImpl;
    import com.tool.AppContext;
    
    public class Test {
    
    	public static void main(String[] args) {
    		
    		//InterStudentService stuService=new StudentServiceImpl();
    		
    		InterStudentService stuService=AppContext.getBean(StudentServiceImpl.class);
    		
    		System.out.println(stuService.showStudentList());
    		
    	}
    	
    }
    

    8、结果

    展开全文
  • Spring boot Mybatis 整合(完整版)

    万次阅读 多人点赞 2017-08-16 15:59:47
    Mybatis注解方式有兴趣的,可以查看我的另一篇:Spring boot Mybatis 整合(注解版) 注解方式更加简洁简单,极大程度的提高了开发速度。如果大家需要使用更高的版本比如springboot2.0,请参照我最新的博客...
  • SpringMVC Spring Mybatis整合步骤

    千次阅读 2018-01-12 15:44:27
    配置mybatis  1.在pom.xml中导入mybatis3.4.5的依赖,导入mysql-connector-java-5.1.43    2.写mybatis-config-xml的配置文件,里面包含连接数据库的信息和映射文件  3.实体类和对应的映射文件(person,...
  • 使用mybatis3.1.1+spring3.2.0+spring-mybatis+1.3.0(或者1.1.1)在 使用 配置时测试失败 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [E:\...
  • Document root element "beans", must match DOCTYPE root "null".
  • spring mybatis 整合后mapper接口注入失败问题

    万次阅读 热门讨论 2016-04-26 18:10:21
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fkhd.whiteshirt.daos.UserMapper] found for dependency: expected at least 1 bean which qualifies as auto
  • spring-mybatis整合jar包spring-mybatis整合jar包spring-mybatis整合jar包
  • 将此jar包更换与客户端相匹配的jar包就可以啦
  • Spring boot Mybatis 整合(注解版)

    万次阅读 多人点赞 2017-11-24 10:39:57
    之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦。接口定义和映射离散在不同的文件中,阅读起来不是很方便。...
  • MyBatis详解:springmybatis整合 目录 MyBatis详解:springmybatis整合 一、整合思路 二、环境 2.1、jar包 三、springmybatis整合后原始dao开发 3.1、建立mybatis的xml文件SqlMapConfig.xml 3.2、在...
  • 而mybatis的setting往往使用默认配置,所以我们经常配置datasource数据源与mappers映射,但学习spring之后发现,对于datasource的配置交由spring进行管理,所以在springmybatis整合后mybatis的配置文件中将不需要...
  • spring-webmvc,mybatis,mybatis-spring ojdbc,dbcp,spring-jdbc,junit step2.添加Spring配置文件 注:不再需要MyBatis的配置文件,MyBatis相关的配置用一个 bean来代替(SqlSessionFactoryBean)。 ...
  • Springmybatis整合

    2017-11-06 14:51:16
    SpringMybatis整合

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 41,306
精华内容 16,522
关键字:

springmybatis整合

spring 订阅