1. db.properties
和log4j.properties
這里是對數(shù)據(jù)庫的參數(shù)進(jìn)行一些配置,為了防止硬編碼不利于系統(tǒng)的優(yōu)化。(在與springmvc整合后,由spring管理)
2. SqlMapConfig.xml
配置
只需定義別名
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 定義別名。配置批量掃描po包 -->
<typeAliases>
<!-- 批量別名定義。指定包路徑,mybatis自動掃描包下邊的pojo類,自動定義別名,別名默認(rèn)為類名(首字母小寫或大寫) -->
<package name="po" />
</typeAliases>
</configuration>
解釋
<typeAliases>
<!-- <typeAlias type="zq.mybatis.test1.User" alias="_User"/> -->
<package name="zq.mybatis.test1"/>
</typeAliases>
使用package設(shè)置別名的手該如何運(yùn)用這個別名呢?很簡單,我指定了他的包名,那這個包下面的所有實(shí)體相當(dāng)于已經(jīng)被設(shè)置了別名,而這個別名實(shí)際上就是某一個實(shí)體自己的實(shí)體名。
在對應(yīng)的映射文件中將使用別名:
<update id="updateUser" parameterType="User"> <!-- parameterType="User"就是使用了別名User -->
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getAllUser" resultType="User"> <!-- resultType="User"就是使用了別名User -->
select * from users
</select>
3. applicationContext.xml
配置數(shù)據(jù)源
、SqlsessionFactory
、MapperScannerConfigurer
(mapper掃描器)
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-------------------------------1.dao層------------------------------->
<!-- 1.配置數(shù)據(jù)源 -->
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 數(shù)據(jù)庫連接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 2.SqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!--3.MapperScannerConfigurer:mapper的掃描器,將包下邊的mapper接口自動創(chuàng)建代理對象,
自動創(chuàng)建到spring容器中,bean的id是mapper的類名(首字母小寫) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-------------------------------2.service層------------------------------->
<!--配置service的實(shí)現(xiàn)class-->
<!-- 商品管理的service -->
<bean id="itemsService" class="service.impl.ItemsServiceImpl"/>
<!-------------------------------3.transaction層------------------------------->
<!-- 事務(wù)管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* service.impl.*.*(..))"/>
</aop:config>
</beans>
4. 使用myBatis-generator工具產(chǎn)生對應(yīng)的mapper.java
接口和mapper.xml
實(shí)現(xiàn)
分別粘貼到cn.itcast.ssm.po
和cn.itcast.ssm.mapper
包下
5. Junit測試
ItemsMapperTest.java
public class ItemsMapperTest {
private ApplicationContext applicationContext;
private ItemsMapper itemsMapper;
@Before
public void setUp() throws Exception {
// 創(chuàng)建spring容器
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
}
// 根據(jù)主鍵查詢
@Test
public void testSelectByPrimaryKey() {
Items items = itemsMapper.selectByPrimaryKey(1);
System.out.println(items);
}
}
6. po和mapper重構(gòu)
繼續(xù)在po下增加ItemsCustom.java
、ItemsQueryVo.java
在mapper下增加ItemsMapperCustom.java
、ItemsMapperCustom.xml
7. 完成service模塊
UserService.java
a
UserServiceImpl.java
b
8. 完成controller模塊
ItemsController.java
9. 配置springmvc.xml
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 1.使用spring組件掃描 --><!-- 開啟注解掃描功能 -->
<context:component-scan base-package="controller" />
<!-- 2.通過annotation-driven可以替代下邊的處理器映射器和適配器 -->
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
<!--*注解處理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
<!-- *注解適配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- 在webBindingInitializer中注入自定義屬性編輯器、自定義轉(zhuǎn)換器 -->
<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 3.配置視圖解析器ViewResolver。要求將jstl的包加到classpath -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
10. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置spring容器監(jiān)聽器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加載springmvc配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件的地址 如果不配置contextConfigLocation,默認(rèn)查找的配置文件名稱classpath下的:servlet名稱+"-serlvet.xml"即:springmvc-serlvet.xml -->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>