Spring4整合MyBatis3 (1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yikong2yuxuan/article/details/62429639

开发环境配置:

1.安装jdk8 2.安装eclipse 3.安装Tomcat8 4.安装MySql5

准备数据库资源:

#创建用户表
create table tb_user(
id int primary key auto_increment,#id
loginname varchar(50)  unique,    #登录名 邮箱
       PASSWORD VARCHAR(18),          #密码
      username     VARCHAR(18),          #用户名
      phone          varchar(18),              #电话
     address        varchar(255)             #地址
);
INSERT INTO tb_user(loginname,PASSWORD,username,phone,address) values('jack','123456','杰克','13920001234',广州市天河区)


CREATE TABLE tb_book(
id INT (11)  PRIMARY KEY AUTO_INCREMENT,
name  VARCHAR(54),
        author  VARCHAR(54),
        publicationdate  DATE,
        publication VARCHAR(150),
price    double;
  image varchar(54),
remark  varchar(600)  
)
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('1','疯狂java讲义(附光盘)','李刚 编著','2008-10-01','电子工业出版社','74.2','java.jpg','疯狂源自梦想');
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('2','疯狂ajax讲义(附光盘)','李刚 编著','2009-10-01','电子工业出版社','84.2','java.jpg','疯狂源自梦想,是啊');
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('3','疯狂xml讲义(附光盘)','李刚 编著','2010-10-01','电子工业出版社','94.2','java.jpg','疯狂源自梦想,必须的');
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('4','疯狂ios讲义(附光盘)','李刚 编著','2011-10-01','电子工业出版社','104.2','java.jpg','疯狂源自梦想,肯定的')

完成配置文件:

新建一个项目fkbookapp,加入jar包

fkbookapp/src/db.properties

dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/mybatis
dataSource.user=root
dataSource.password=root
dataSource.maxPoolSize=20
dataSource.maxIdleTime=1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5
  fkbookapp/WebContent/WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
	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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
			            http://www.springframework.org/schema/context
			            http://www.springframework.org/schema/context/spring-context-4.2.xsd
			            http://www.springframework.org/schema/mvc
			            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
			            http://www.springframework.org/schema/tx
			            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
			            http://mybatis.org/schema/mybatis-spring 
			            http://mybatis.org/schema/mybatis-spring.xsd ">
		
		
		<!-- mybatis:scan会将org.fkit.mapper包里的所有接口当做mapper配置,之后可以自动引入mapper类 -->
		<mybatis:scan base-package="org.fkit.mapper"/>
		<!-- 扫描org.fkit包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring 的bean -->
		<context:component-scan base-package="org.fkit" />
		<!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 -->
		<context:property-override location="classpath:db.properties"/>
		<!-- 配置C3p0数据源 -->
		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>
		<!-- 配置SqlSessionFacotroy,org.mybatis.spring.SqlSessionFactoryBean是MyBatis社区开发用于整合Spring的Bean -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" />
		<!-- JDBC事务管理器 -->
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
		<!-- 启用支持annotation注解方式事务管理 -->
		<tx:annotation-driven transaction-manager="transactionManager" /> 
 </beans>

fkbookapp/WebContent/WEB-INF/springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd     
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        
   	 <!-- 自动扫描该包,springMVC会将包下用了@controller注解的类注册为Spring的controller -->
    	<context:component-scan base-package="org.fkit.controller"/>
     <!-- 设置默认配置方案 -->
    	<mvc:annotation-driven/>
     <!-- 视图解析器 -->
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<!-- 前缀 -->
    		<property name="prefix">
    			<value>/WEB-INF/content/</value>
    		</property>
    		<!-- 后缀 -->
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
</beans>

猜你喜欢

转载自blog.csdn.net/yikong2yuxuan/article/details/62429639