本项目搭建用到maven仓库,可以方便jar包的管理,如果不用maven管理可以直接新建web项目。然后把jar包拷到web-inf的lib包下即可,这是两者之间的区别
1. 配置maven见以前的总结
2. 新建maven项目,目录结构如下,src/main/java中写的是开发java代码,src/main/resources里写的是配置文件,src/main/webapp下相当于以前的webcontent目录,最后的pom.xml文件中配置本框架需要的jar包坐标依赖。
3. 这里dao层只写到接口,无需实现类,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句,如下:注意保持各项名称一致需要映射
Dao接口层
@Repository
public interface IPersonDao {
List<Person> selall();
void insertPerson(Person person);
}
实体xml文件
<mapper namespace="group.dao.IPersonDao"这里对应dao层的接口路径><!-- 独一无二的命名空间 -->
<!-- 定义查询 select标签,id 用来区分语句,resultType表示返回类型-->
<select id="selall这里对应方法名" resultType="group.entity.Person">
select * from person
</select>
<!-- insert-->
<insert id="insertPerson" parameterType="group.entity.Person">
insert into person values(#{name},#{id},#{address})
</insert>
</mapper>
配置文件
Spring-mvc
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!—为了不在xml中配置类,用来扫描注解注入bean对象 -->
<context:component-scan base-package="group.controller"/>
<!-- 首先支持mvc的注解,同时我们使用的spring是4.x版本。如果打上这个标签自动注入两个bean
requesthandlermapping,requesthandleradapter
-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 让对静态资源的请求能够绕过dispatcherServlet的拦截 -->
<mvc:default-servlet-handler/>
<!-- 视图转换器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
与mybatis结合spring-mybatis.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: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-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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<!-- 默认的注解映射的支持 --><mvc:annotation-scan/>是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。
<!-- 开启事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 引入配置文件 -->
<!-- 官方解释A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.
The default placeholder syntax follows the Ant / Log4J / JSP EL style:
${...} -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:group/entity/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="group.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 自动扫描 注解这里扫描除了controller包之外的包,因为controller里存在dao,service,如果扫描将会自动生成dao或者service对象,这时生成的对象没有数据库信息,没有用,所以不能扫描controller包-->
<context:component-scan base-package="group">
<context:exclude-filter type="regex" expression="group.controller.*"/>
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> -->
</context:component-scan>
</beans>
Spring.xml,为了定义spring-mybatis.xml与mvc.xml的读取顺序,先加载spring-mybatis.xml再加载spring-mvc.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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="spring-mybatis.xml"/>
<import resource="springmvc.xml"/>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
Mvc中央控制器
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>定义文件读取顺序
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>