• MyBatis入门程序


    MyBatis入门程序

    设计案例

    实现向tedu_ums数据库的t_users数据表中插入数据。

    开发步骤

    1 创建项目DAY07-MyBatis-Sample,生成web.xml,在web.xml中配置(2项),添加spring-webmvc依赖,复制Spring配置文件,添加Tomcat运行环境;

    2 添加新的依赖:mybatismybatis-springspring-jdbc

    <!-- Spring-JDBC -->
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>3.2.8.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>org.mybatis</groupId>
    	<artifactId>mybatis</artifactId>
    	<version>3.4.6</version>
    </dependency>
    
    <dependency>
    	<groupId>org.mybatis</groupId>
    	<artifactId>mybatis-spring</artifactId>
    	<version>1.3.2</version>
    </dependency>
    

    以上依赖中,spring-jdbc的版本需与spring-webmvc保持一致!(同一个项目中,以spring-作为前缀的依赖都应该使用相同版本)

    3 创建实现类cn.tedu.spring.entity.User,在该类中声明Integer idString usernameString password属性,及相关方法;

    4 设计持久层接口:创建cn.tedu.spring.mapper.UserMapper接口,并在接口中添加抽象方法:

    Integer reg(User user);
    

    注意:增删改操作固定返回受影响的行数,在设计方法时,可以将返回值声明为Integer类型,或使用void表示无返回值也可以。

    5 从FTP下载mybatis.zip文件,将解压得到的EmpMapper.xml重命名为UserMapper.xml(推荐这样命名),然后在项目的srcmain esource新建文件夹mappers,将UserMapper.xml复制到mappers文件夹中;

    6 编辑UserMapper.xml文件:

    <!-- MyBatis的接口映射文件,根节点是mapper -->
    <!-- 接口映射文件是与Java接口文件(interface)相对应的 -->
    <!-- 根节点的namespace属性用于指定Java接口文件 -->
    <mapper 
    	namespace="cn.tedu.spring.mapper.UserMapper">
    	
    	<!-- 节点名称取决于需要执行的操作 -->
    	<!-- 例如增加操作应该使用insert节点 -->
    	<!-- id属性(*)的值是Java接口中的方法名称 -->
    	<!-- parameterType属性的值是参数类型 -->
    	<!-- 节点中间编写SQL语句 -->
    	<insert id="reg"
    		parameterType="cn.tedu.spring.entity.User">
    		INSERT INTO t_users (
    			username, password
    		) VALUES (
    			#{username}, #{password}
    		)
    	</insert>
    	
    </mapper>
    

    7 添加依赖:mysql-connector-xxxxdbcpjunit

    8 在srcmain esources下配置db.properties

    9 从此前的项目中复制spring-dao.xml,在该配置文件中,至少配置了:加载db.propertiesBasicDataSource

    10 在spring-dao.xml中,配置:

    <!-- 配置MapperScannerConfigurer ,扫描mapper的配置在哪里-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<!-- 配置接口文件所在的包 -->
    	<property name="basePackage"
    		value="cn.tedu.spring.mapper" />
    </bean>
    
    <!-- 配置SqlSessionFactoryBean -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!-- 配置数据源:如何连接数据库等 -->
    	<property name="dataSource"
    		ref="dataSource" />
    	<!-- 配置XML文件的位置 -->
    	<property name="mapperLocations"
    		value="classpath:mappers/UserMapper.xml" />
    </bean>
    

    11 编写测试类,添加测试方法:

    @Test
    public void testReg() {
    	AbstractApplicationContext ac
    		= new ClassPathXmlApplicationContext(
    			"spring-mvc.xml",
    			"spring-dao.xml");
    	
    	UserMapper userMapper
    		= ac.getBean(
    			"userMapper", UserMapper.class);
    	
    	User user = new User();
    	user.setUsername("Tom1");
    	user.setPassword("123456");
    	Integer affectedRows
    		= userMapper.reg(user);
    	System.out.println(
    		"affectedRows=" + affectedRows);
    	
    	ac.close();
    }
    

    总结

    • 在spring中配置mybatis的步骤:

      • 配置扫描xxmapper.xmlMapperScannerConfigurer
      • 配置SqlSessionFactoryBean,需要用到数据库连接池的数据源datasource
    • 完整的依赖文件pom.xml

    <dependencies>
    
    		<!-- SpringMVC -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>3.2.8.RELEASE</version>
    		</dependency>
    		
    		<!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>3.2.8.RELEASE</version>
    		</dependency>
    
    		<!-- MyBatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.4.6</version>
    		</dependency>
    
    		<!-- MyBatis-Spring -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    			<version>1.3.2</version>
    		</dependency>
    		
    		<!-- MySQL -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.28</version>
    		</dependency>
    
    		<!-- DBCP -->
    		<dependency>
    			<groupId>commons-dbcp</groupId>
    			<artifactId>commons-dbcp</artifactId>
    			<version>1.4</version>
    		</dependency>
    
    		<!-- Junit -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.12</version>
    		</dependency>
    
    	</dependencies>
    
    
    • 完整的db.properties文件
    url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
    driver=com.mysql.jdbc.Driver
    user=root
    password=root
    initSize=5
    maxSize=10
    
    • 完整的spring-dao.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:context="http://www.springframework.org/schema/context" 
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    	xmlns:jee="http://www.springframework.org/schema/jee" 
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    		
    	<!-- 加载db.properties -->
    	<util:properties id="dbConfig"
    		location="classpath:db.properties" />
    	
    	<!-- 数据源 -->
    	<bean id="dataSource"
    		class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="url" 
    			value="#{dbConfig.url}" />
    		<property name="driverClassName" 
    			value="#{dbConfig.driver}" />
    		<property name="username" 
    			value="#{dbConfig.user}" />
    		<property name="password" 
    			value="#{dbConfig.password}" />
    		<property name="initialSize" 
    			value="#{dbConfig.initSize}" />
    		<property name="maxActive" 
    			value="#{dbConfig.maxSize}" />
    	</bean>
    
    	<!-- 配置MapperScannerConfigurer,自动扫描整个包,并且spring会自动创建UserMapper接口对象-->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<!-- 配置接口文件所在的包 -->
    		<property name="basePackage"
    			value="cn.tedu.spring.mapper" />
    	</bean>
    	
    	<!-- 配置SqlSessionFactoryBean -->
    	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    		<!-- 配置数据源:如何连接数据库等 -->
    		<property name="dataSource"
    			ref="dataSource" />
    		<!-- 配置XML文件的位置 -->
    		<property name="mapperLocations"
    			value="classpath:mappers/UserMapper.xml" />
    	</bean>
    </beans>
    
  • 相关阅读:
    多项式乘法
    容斥计算多重组合
    D. Tokitsukaze, CSL and Stone Game
    优惠买商品(dp、greedy)
    数星星(单点更新,求前缀和)
    信息推送(单点更新,求前缀和)
    互相送礼物
    Codeforces Round #611 (Div. 3)E. New Year Parties
    多源bfs
    mysql事务和锁
  • 原文地址:https://www.cnblogs.com/Chenjiabing/p/9010363.html
Copyright © 2020-2023  润新知