Maven入门(Myeclipse下)-第二节

注意:Myeclipse和sts自带maven支持,eclipse需要安装m2eclipse,我目前使用的版本是myeclipse10

一.Myeclipse设置自定义的maven以及仓库

进入Preferences属性进行设置:

NO1:

Maven入门(Myeclipse下)-第二节_第1张图片

NO2:

Maven入门(Myeclipse下)-第二节_第2张图片

NO3:

Maven入门(Myeclipse下)-第二节_第3张图片

二.尝试导入一个既有的maven工程

Maven入门(Myeclipse下)-第二节_第4张图片

三.用户模块中的核心模块作为maven项目的入门,访问数据库使用ibatis

开始编码之前,我们必须知道一下几个重要的概念:

概念介绍

1.依赖包的查询:

1.所有依赖都是通过坐标来进行存储的(GAV:groupId,artifactId,version)
2.提供GAV的查询:http://mvnrepository.com
3.通过<dependencies>设置依赖

2.依赖的传递性

<scope>来定义传递的范围
1.默认scope:compile
B依赖A,C如果依赖B的话,那么A所依赖的包将全部传递给C。在编译和打包时有效,在编译和打包时都将其存储进去
2.scope:test
基于测试,测试范围有效,在编译和打包时不会使用这个依赖,不传递。打成war时不将其打入包中;
3.scope:provided
编译和测试的过程有效,打成war包时不会加入。为了避免冲突,例如servlet-api,tomcat已经存在,如果在打包的会冲突
4.scope:runtime
编译时无效,运行时依赖

项目流程

项目结构:
Maven入门(Myeclipse下)-第二节_第5张图片


1.新建项目

Step1:
Maven入门(Myeclipse下)-第二节_第6张图片
Step2:
Maven入门(Myeclipse下)-第二节_第7张图片


2.pom中依赖属性的配置
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.techbirds.user</groupId>
	<artifactId>user-core</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>user-core</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-sqlmap</artifactId>
			<version>2.3.4.726</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.1</version>
		</dependency>
	</dependencies>
</project>

3.实体类的编写
package com.techbirds.vo;

public class User {
	private Integer userId;
	private String username;
	private String password;
	private String mobile;
	private String email;

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String toString() {
		return "User [email=" + email + ", mobile=" + mobile + ", password="
				+ password + ", userId=" + userId + ", userName=" + username
				+ "]";
	}
}

4.资源文件的配置
jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ibatis?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

SqlMapConfig.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig        
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"        
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<properties resource="jdbc.properties" />  
    <transactionManager type="JDBC">  
        <dataSource type="SIMPLE">  
            <property name="JDBC.Driver" value="${jdbc.driverClassName}" />  
            <property name="JDBC.ConnectionURL" value="${jdbc.url}" />  
            <property name="JDBC.Username" value="${jdbc.username}" />  
            <property name="JDBC.Password" value="${jdbc.password}" />  
        </dataSource>  
    </transactionManager>  
    <sqlMap resource="User.xml" />  
</sqlMapConfig> 

User.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap        
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">  
<sqlMap>  
	<typeAlias alias="User" type="com.techbirds.vo.User" />
	  
	<select id="QueryUserList" resultClass="User">  
	    select * from users  
	</select>
	
	<select id="QueryUserById" resultClass="User" parameterClass="int">
		select * from users where userId=#value#
	</select>
	
	<insert id="AddUser" parameterClass="User">  
		insert into users(username,password,mobile,email) values(#username#,#password#,#mobile#,#email#)  
	</insert>  
	
	<update id="UpdateUser" parameterClass="User">
		update users set username=#username# where userId=#userId#   
	</update>
	 
	<delete id="DeleteUserById" parameterClass="int">
		delete from users where userId=#value#   	
	</delete>
</sqlMap>  



5.测试类的编写

package com.techbirds.vo;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;

import org.junit.Before;
import org.junit.Test;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;


public class UserTest {
	
	private Logger logger=Logger.getLogger(UserTest.class.getName());
	
	private Reader reader=null;
	private  SqlMapClient sqlMap=null;
	
	@Before
	public void init() throws IOException{
		String config = "SqlMapConfig.xml";  
        reader = Resources.getResourceAsReader(config);
        sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	}
	
	@Test
	public void getAllUsers() throws SQLException{
		@SuppressWarnings("unchecked")
		List<User> users=sqlMap.queryForList("QueryUserList");
		for (User user : users) {
			logger.info(user.toString());
		}
	}
	
	
}



6.执行项目

Maven入门(Myeclipse下)-第二节_第8张图片


ps:对于ibatis的maven整合,我想记录一点,在maven项目中的资源文件假如全部存放至resources中去,那么不管是代码引用资源文件还是资源文件引入资源文件,直接引用便可。

demo下载: http://download.csdn.net/detail/techbirds_bao/5605325

你可能感兴趣的:(maven,MyEclipse)