1、win+r cmd 查看maven是否安装 mvn -v 出现以下信息为安装成功
2、如若未安装 安转如下:
http://maven.apache.org/docs/ 选取自己安装的版本
下载完成后解压成功
设置D:\maven\apache-maven-3.3.9\conf 下的settings.xml 新建文件夹(F:/maven/pro)设置中央仓库的路径
配置maven的环境变量
MAVEN_HOME =D:\maven\apache-maven-3.3.9
PATH=%MAVEN_HOME%\bin;
安装成功
3 myEclipse 使用maven开发项目
http://www.itnose.net/detail/6074493.html ;参考地址,个人具体配置如下:
1、Window--preference-->MyEclipse-->Maven4MyEclipse-->Installations/User Settings
2、新建项目
New-->other-->MyEclipse-->Maven4MyEclipse-->Maven Project 或者
New-->web Project-->
1、在项目下添加文件夹(Source文件夹)项目右键-->New-->Source Folder
目录结构
2、修改Build Path 项目右键-->Build Path-->Configure Build Path
Target/classes 为项目的编译后生成的class文件。
3、项目右键-->MyCelipse-->Project Facets
项目准备工作完成
1、目录结构
2、spring与mybatis 的整合 Jdbc.properties/spring-mybatis.xml
注意:Jdbc.properties的内容中禁止出现空格
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.0.66\:3306/datashareplatdb
username=root
password=root
validationQuery=SELECT 1
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
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"
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">
<!-- 自动扫描 -->
<context:component-scan base-package="com.yy"/>
<!-- 引入数据库连接配置文件 -->
<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="0"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="20"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000"></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:mapper/*Mapper.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yy.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>
</beans>
Spring-mybatis的配置完成
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 该spring-mybatis的配置主要是 自动扫描控制器,视图模式,注解的启动 -->
<!-- 添加注解驱动 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- 设置使用注解的类所在的包 -->
<context:component-scan base-package="com.yy.controller" />
<!-- 完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
spring-mvc.xml配置完成。
Java 代码编写
Dao层(注意:生成的方法与UserMapper.xml的对应的f方法id值对应,个数也要一致)
package com.yy.dao;
import com.yy.entity.User;
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
Service层
package com.yy.service;
import com.yy.entity.User;
public interface UserService {
User getUser(Integer id);
void addUser(User user);
void deleteUser(Integer id);
}
ServiceImpl层
package com.yy.service.serviceImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.yy.dao.UserMapper;
import com.yy.entity.User;
import com.yy.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
private UserMapper dao;
@Override
public User getUser(Integer id) {
return dao.selectByPrimaryKey(id);
}
@Override
public void addUser(User user) {
dao.insert(user);
}
@Override
public void deleteUser(Integer id) {
dao.deleteByPrimaryKey(id);
}
}
UserController.java controller层
package com.yy.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yy.entity.User;
import com.yy.service.UserService;
@Controller
@RequestMapping("user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/getUser")
public String getUser( HttpServletRequest request ,Model model){
Integer id = Integer.parseInt(request.getParameter("id"));
User user = userService.getUser(id);
model.addAttribute("user", user);
return "showuser";
}
@RequestMapping("/addUser")
public void addUser( HttpServletRequest request ,Model model){
User user = new User();
user.setId(2);
user.setAge(12);
user.setPassword("123456");
user.setUserName("zhangsan");
userService.addUser(user);
System.out.println("添加成功");
}
@RequestMapping("/deleteUser")
public void deleteUser( HttpServletRequest request ,Model model){
Integer id = Integer.parseInt(request.getParameter("id"));
userService.deleteUser(id);
System.out.println("删除成功");
}
}
Src/main/resources/mapper/UserMapper.xml(注意:mapper/UserMapper.xml 一定要放在src/main/resource/目录下,否则*Mapper.xml文件读取不到)
Pom.xml的配置信息(注意:如果pom.xml报红叉,内部内容没有报错,有两个种可能的错误:
1、修改build Path 下的系统库的jdk 的版本为jdk1.7
2、Pom.xml的内容一定出错,检查每个标签的内容值。可定有引入错的
)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.yy.mavenPro</groupId>
<artifactId>mavenPro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name/>
<url>http://maven.apache.org</url>
<description/>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring版本号 -->
<spring.version>4.0.4.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>mavenPro</finalName>
</build>
</project>
最后 项目右键--->Run As--->Maven Clean-->Maven Install
(注意:Maven Clean/Maven Install报错可能 jdk的版本问题,去build Path 下修改 library 为jdk1.7)
心得:以上注意的地方为自己所遇见的问题。最重要的是如果控制台出现问题,不要着急,静下心寻求问题原因
1.spring-mybatis.xml,spring-mvc.xml的目录结构是否存在。
2.dao层*Mapper.java内生成的方法一定要与对应的*Mapper.xml的方法个数相同。.java内的方法名与.xml的id的值相同。
3.项目的Build Path 的 configuration的配置(特别是版本)。