强烈推荐一个大神的人工智能的教程:/zhanghan
一、创建zh-root项目(用来管理依赖包的版本)
1.使用idea开发工具新建maven项目,坐标如下:
-
<groupId></groupId>
-
<artifactId>zh-root</artifactId>
-
<version>1.0-SNAPSHOT</version>
2.添加对应依赖及版本
-
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
-
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
-
-
<modelVersion>4.0.0</modelVersion>
-
<groupId></groupId>
-
<artifactId>zh-root</artifactId>
-
<version>1.0-SNAPSHOT</version>
-
<packaging>pom</packaging>
-
-
<properties>
-
<!--构建项目时使用,用于maven中的文件拷贝-->
-
<>UTF-8</>
-
<>UTF-8</>
-
<!-- 编译时的编码 -->
-
<!--否则在dos执行mvn compile可能由于编码问题出现莫名其妙的问题,因为dos默认的编码,格式是系统的GBK-->
-
<>UTF-8</>
-
<!-- 编译源代码 jdk版本编译 -->
-
<>1.8</>
-
<!-- 编译源代码 版本 -->
-
<>1.8</>
-
<!-- 编译插件版本号控制 -->
-
<>3.7.0</>
-
<!--junit单元测试 -->
-
<>4.12</>
-
<!--日志-->
-
<>1.2.17</>
-
<>5.0.</>
-
<>1.8.13</>
-
<>2.9.2</>
-
<>3.4.5</>
-
<>1.3.1</>
-
<>5.1.38</>
-
<>1.1.6</>
-
</properties>
-
-
<dependencyManagement>
-
<dependencies>
-
<!--测试-->
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--日志-->
-
<dependency>
-
<groupId>log4j</groupId>
-
<artifactId>log4j</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--spring-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-core</artifactId>
-
<version>${}</version>
-
</dependency>
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-context</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--JDBC-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-jdbc</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--spring事务-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-tx</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--Spring面向切面编程-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-aop</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--spring-aop的依赖-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>aspectjweaver</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--spring-web的依赖-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-web</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--spring-mvc-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-webmvc</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--spring-mvc-json-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>jackson-databind</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--mybatis-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>mybatis</artifactId>
-
<version>${}</version>
-
</dependency>
-
<dependency>
-
<groupId></groupId>
-
<artifactId>mybatis-spring</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!--mysql-->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
<version>${}</version>
-
</dependency>
-
<!-- 连接池 -->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>druid</artifactId>
-
<version>${}</version>
-
</dependency>
-
</dependencies>
-
</dependencyManagement>
-
-
<build>
-
<plugins>
-
<plugin>
-
<!-- 用来编译位于src/main/java/目录下的主源码 -->
-
<groupId></groupId> <!-- 项目或者组织的唯一标志 -->
-
<artifactId>maven-compiler-plugin</artifactId> <!-- 项目的通用名称 -->
-
<version>${}</version>
-
<!-- 使用maven-compiler-plugin 指定JDK版本和编码 -->
-
<configuration> <!-- 通常用于私有不开源的plugin -->
-
<source>${}</source> <!-- 编译源代码 jdk版本编译 -->
-
<target>${}</target>
-
<encoding>${}</encoding> <!-- 用于maven中的文件拷贝 的 编码格式 -->
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
-
</project>
二、创建mybatis-spring-zh(我使用的是quickstart)
1.使用idea开发工具新建maven项目.坐标如下:
-
<groupId>-spring</groupId>
-
<artifactId>mybatis-spring</artifactId>
-
<version>1.0-SNAPSHOT</version>
2.修改中的打包方式为war
<packaging>war</packaging>
3.新建文件(目录:src/main/webapp/WEB-INF/),添加约束如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns="/xml/ns/javaee"
-
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"
-
version="3.0">
-
-
</web-app>
4.在中添加相关依赖
-
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
-
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
-
<parent>
-
<groupId></groupId>
-
<artifactId>zh-root</artifactId>
-
<version>1.0-SNAPSHOT</version>
-
</parent>
-
-
<modelVersion>4.0.0</modelVersion>
-
<groupId>-spring</groupId>
-
<artifactId>mybatis-spring</artifactId>
-
<version>1.0-SNAPSHOT</version>
-
<packaging>war</packaging>
-
-
<dependencies>
-
<!--测试-->
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<scope>test</scope>
-
</dependency>
-
-
<!--日志-->
-
<dependency>
-
<groupId>log4j</groupId>
-
<artifactId>log4j</artifactId>
-
</dependency>
-
-
<!--Spring核心-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-core</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-context</artifactId>
-
</dependency>
-
-
<!--Spring JDBC-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-jdbc</artifactId>
-
<version>5.0.</version>
-
</dependency>
-
-
<!--Spring事务-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-tx</artifactId>
-
</dependency>
-
-
<!--Spring面向切面编程-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-aop</artifactId>
-
</dependency>
-
-
<!--spring-aop依赖-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>aspectjweaver</artifactId>
-
</dependency>
-
-
<!--Spring-MVC依赖-->
-
<!--Spring web依赖-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-web</artifactId>
-
</dependency>
-
<!--Spring-MVC-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>spring-webmvc</artifactId>
-
</dependency>
-
-
<!--spring-mvc-json-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>jackson-databind</artifactId>
-
</dependency>
-
-
<!--spring-mybatis-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>mybatis</artifactId>
-
</dependency>
-
-
-
<dependency>
-
<groupId></groupId>
-
<artifactId>mybatis-spring</artifactId>
-
</dependency>
-
-
<!--mysql-->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
</dependency>
-
<!--数据库连接池-->
-
<dependency>
-
<groupId></groupId>
-
<artifactId>druid</artifactId>
-
</dependency>
-
</dependencies>
-
</project>
5.项目的完整路径如下,第一步先创建项目的基本结构,controller,mapper,model,service
6.创建springmvc的配置文件,配置自动扫描
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="/schema/beans"
-
xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns:context="/schema/context"
-
xmlns:mvc="/schema/mvc"
-
xsi:schemaLocation="/schema/beans
-
/schema/beans/
-
/schema/mvc
-
/schema/mvc/spring-mvc-4.
-
/schema/context
-
/schema/context/">
-
-
-
<mvc:annotation-driven/>
-
-
<context:component-scan base-package=""/>
-
<context:component-scan base-package=""/>
-
-
<bean class="">
-
<!--<property name="viewClass" value=""/>-->
-
<property name="prefix" value="/WEB-INF/jsp/"/>
-
<property name="suffix" value=".jsp"/>
-
</bean>
-
-
<import resource="classpath:spring/"/>
-
<import resource="classpath:spring/"/>
-
</beans>
7.创建文件,配置数据库连接的相关参数
-
jdbc_driverClassName=
-
jdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
-
jdbc_username=root
-
jdbc_password=
8.创建,连接数据库配置
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="/schema/beans"
-
xmlns:xsi="http:///2001/XMLSchema-instance"
-
xsi:schemaLocation="/schema/beans
-
/schema/beans/">
-
-
<!--数据库连接设置-->
-
<!--<bean class="com..P6DataSource">-->
-
<!--<constructor-arg>-->
-
<!--<ref bean="dataSourceDefault"/>-->
-
<!--</constructor-arg>-->
-
<!--</bean>-->
-
-
<bean id="dataSource" class="" destroy-method="close">
-
<property name="url" value="${jdbc_url}"/>
-
<property name="username" value="${jdbc_username}"/>
-
<property name="password" value="${jdbc_password}"/>
-
</bean>
-
</beans>
9.创建mybatis配置文件,用于mybatis的常用配置
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/">
-
<configuration>
-
<settings>
-
<setting name="logImpl" value="LOG4J"/>
-
<setting name="cacheEnabled" value="true"/>
-
<setting name="mapUnderscoreToCamelCase" value="true"/>
-
<setting name="aggressiveLazyLoading" value="false"/>
-
</settings>
-
</configuration>
10.创建mybatis和spring整合的配置文件,
-
<?xml version="1.0" encoding="UTF-8"?>
-
-
<beans xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns="/schema/beans"
-
xmlns:context="/schema/context"
-
xsi:schemaLocation="/schema/beans
-
/schema/beans/
-
/schema/context
-
/schema/context/">
-
<!--加载db配置文件-->
-
<context:property-placeholder location="classpath:"/>
-
-
<!--配置sqlSessionFactory-->
-
<bean id="sqlSessionFactory" class="">
-
<property name="configLocation" value="classpath:spring/"/>
-
<property name="dataSource" ref="dataSource"/>
-
<property name="mapperLocations">
-
<array>
-
<value>classpath:mapper/*.xml</value>
-
</array>
-
</property>
-
<property name="typeAliasesPackage" value=""/>
-
</bean>
-
-
<!--扫描mapper-->
-
<bean class="">
-
<property name="basePackage" value=""/>
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
-
</bean>
-
</beans>
11.完善
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns="/xml/ns/javaee"
-
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"
-
version="3.0">
-
<context-param>
-
<!--定义了要装入的spring配置文件-->
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:spring/</param-value>
-
</context-param>
-
-
<listener>
-
<listener-class></listener-class>
-
</listener>
-
<!--前端控制器-->
-
<servlet>
-
<servlet-name>mybatis</servlet-name>
-
<servlet-class>
-
-
</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:spring/</param-value>
-
</init-param>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>mybatis</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
-
</web-app>
12.写一个小例子测试一下是否成功了,以查询用户为例,分别创建,UserService,UserController,SysUser,访问该测试方法,查询出如下结果。相关程序代码已上传到码云,可以参考,/ZhangHuan8/
关于为什么要抽出一个root项目管理版本,可以参考以下博客:Maven实战(六)--- dependencies与dependencyManagement的区别__Emily的博客-****博客