開發環境配置:
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>