jdbc.properties JDBC配置文件
db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql:///person?characterEncoding=utf-8 db.username=root db.password=root
log4j.properties 日志配置文件
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] %m%n
pom.xml 文件的配置
<!-- 集中定义依赖版本号 --> <properties> <junit.version>4.10</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mysql.version>5.1.32</mysql.version> <druid.version>1.1.6</druid.version> <slf4j.version>1.6.4</slf4j.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <jackson.version>2.4.2</jackson.version> </properties> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 整合spring框架(包含springmvc) 这个jar文件包含springmvc开发时的核心类, 同时也会将依赖的相关jar文件引入进来(spring的核心jar文件也包含在内) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- 整合mybatis框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- 整合log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!--解决对象转json不报406--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.11</version> </dependency> <!-- Servlet/JSP/JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> </dependencies>
web.xml 配置文件(在web-app标签下)
<!--启动项目运行的页面--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置全站500异常跳转页面 --> <error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> <!-- 配置全站404跳转页面 --> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <!-- 配置springmvc, 将所有请求交给springmvc来处理 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springmvc核心配置文件的位置,默认Springmvc的配置文件是在WEB-INF目录下,默认的名字为springmvc-servlet.xml,如果要放在其他目录,则需要指定如下配置: --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/*.xml</param-value> </init-param> </servlet> <!-- 其中的斜杠(/)表示拦截所有请求(除JSP以外), 所有请求都要经过springmvc前端控制器 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 乱码处理过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 指定编码集 --> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <!-- 指定拦截方式为拦截所有请求 --> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 放行静态资源文件 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.jpg</url-pattern> </servlet-mapping>
在resources下创建一个spring文件夹,在spring文件夹中创建两个文件applicationContext.xml、springmvc-config.xml文件
spring
applicationContext.xml(在beans标签下写)
<!-- 1.加载jdbc.properties文件的位置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 配置连接数据库的基本信息 --> <property name="driverClassName" value="${db.driverClassName}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> <!-- 3.整合spring和mybatis框架 将SqlSession等对象的创建交给Spring容器 id值(sqlSessionFactory)是固定值 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 1.指定mybatis核心配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- 2.配置连接池(数据源) ref指向连接池bean对象的id值 --> <property name="dataSource" ref="dataSource"></property> <!-- 3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!-- 5、定义mapper接口扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 --> <property name="basePackage" value="com.demo.dao"/> </bean> <!-- 6.配置需要扫描的包(service层):spring自动去扫描 base-package下的类, 如果扫描到的类上有 @Controller、@Service、@Component等注解, 将会自动将类注册为bean(即由spring创建实例) --> <context:component-scan base-package="com.demo.service"> </context:component-scan>
springmvc-config.xml(beans标签下写 )
```java <!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) --> <mvc:default-servlet-handler/> <!-- 2.配置注解驱动,用于识别注解(比如@Controller) --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类, 如果扫描到的类上有 @Controller、@Service、@Component等注解, 将会自动将类注册为bean --> <context:component-scan base-package="com.demo.controller"> </context:component-scan> <!-- 4.配置内部资源视图解析器 prefix:配置路径前缀 suffix:配置文件后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean>
在resources下创建一个mybatis文件夹中创建一个mapper文件夹和两个文件mybatis-config.xml、mybatis-config2.xml文件
mybatis
mapper 里创建一个xml文件写sql语句
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--映射文件对应 namespace值为对应接口全路径--> <mapper namespace="com.demo.dao.UserMapper"> <!--在mapper根据标签内部可以添加很多SQL标签 select:存放查询类型的SQL语句 intsert、delect、update:存放增/删/改类型的SQL语句 --> <insert id="insertUser"> insert into user values(#{id},#{username},#{password},#{age},#{gender},#{email},#{job},#{sal},#{tel},#{dept}) </insert> <select id="selectByUsername" resultType="com.demo.pojo.User"> select * from user where username=#{username} </select> <select id="selectAll" resultType="com.demo.pojo.User"> select * from user </select> <select id="selectUserByUP" resultType="com.demo.pojo.User"> select * from user where username=#{username} and password=#{password} </select> </mapper>
mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- MyBatis的全局配置文件 --> <configuration > <!-- 1.配置开发环境,标签内可以配置多个环境,比如develop,test等 --> <!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) --> <!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED --> <!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml --> <!-- 以上配置, 与Spring整合后, 都由spring管理 --> </configuration>