Spring+Mybatis+Maven

Spring+Mybatis+Maven(一)

工具:

  jdk1.7

  IDEA2016.2.5
  tomcat6
  oracle10

项目目录结构:

web.xml配置文件

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>    <display-name>Archetype Created Web Application</display-name>    <!-- Spring和mybatis的配置文件 -->    <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring-mybatis.xml</param-value>    </context-param>    <!-- 编码过滤器 -->    <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>    <!-- Spring监听器 -->    <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 防止Spring内存溢出监听器 -->    <listener>      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>    </listener></web-app>

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"       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">    <!-- 自动扫描 service-->
  <context:component-scan base-package="com.sea.**.server">      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  </context:component-scan>
    <!-- 引入配置文件 -->    <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="${driverClassName}" />        <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:com/sea/**/mapper/*.xml"></property>    </bean>    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com/sea/**/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>
UserBean.class
public class UserBean {    private Long userId;    private String name      ;    private String loginName ;    private String password  ;    private int sex       ;    private String phone     ;    private String idCard    ;    private Date createTime;    private String memo      ;

public Long getUserId() {        return userId;    }

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

public String getName() {        return name;    }

public void setName(String name) {        this.name = name;    }

public String getLoginName() {        return loginName;    }

public void setLoginName(String loginName) {        this.loginName = loginName;    }

public String getPassword() {        return password;    }

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

public int getSex() {        return sex;    }

public void setSex(int sex) {        this.sex = sex;    }

public String getPhone() {        return phone;    }

public void setPhone(String phone) {        this.phone = phone;    }

public String getIdCard() {        return idCard;    }

public void setIdCard(String idCard) {        this.idCard = idCard;    }

public Date getCreateTime() {        return createTime;    }

public void setCreateTime(Date createTime) {        this.createTime = createTime;    }

public String getMemo() {        return memo;    }

public void setMemo(String memo) {        this.memo = memo;    }

public UserBean(Long userId, String name,                    String loginName, String password,                    int sex, String phone, String idCard,                    Date createTime, String memo) {        this.userId = userId;        this.name = name;        this.loginName = loginName;        this.password = password;        this.sex = sex;        this.phone = phone;        this.idCard = idCard;        this.createTime = createTime;        this.memo = memo;    }

public UserBean() {    }}
UserDao
public interface UserDao {    /**     * 通过主键查询单个     * @param userId     * @return     */    UserBean getById(Long userId);}
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" ><!--路径不要写错了--><mapper namespace="com.sea.security.dao.UserDao">    <select id="getById" parameterType="java.lang.Long" resultType="com.sea.security.bean.UserBean">        SELECT * FROM t_user where userId = #{userId}    </select></mapper>UserService
public interface UserService {    /**     * 通过主键查询单个     * @param userId     * @return     */    UserBean getById(Long userId);}UserServiceImpl
@Servicepublic class UserServiceImpl implements UserService {    @Resource    private UserDao userDao;

public UserBean getById(Long userId) {        return userDao.getById(userId);    }}Junit单元测试Spring整合Mybatis
import com.sea.security.bean.UserBean;import com.sea.security.server.UserService;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {    private UserService userService;    @Before    public void before(){        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:spring-mybatis.xml"});        userService = (UserService) context.getBean("userServiceImpl");    }

@Test    public void getById(){        UserBean user = userService.getById(1l);        Assert.assertNotNull(user);    }

}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>sea</groupId>  <artifactId>com.workit</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>com.sea Maven Webapp</name>  <url>http://maven.apache.org</url>  <properties>    <!-- spring版本号 -->    <spring.version>4.2.4.RELEASE</spring.version>    <!-- mybatis版本号 -->    <mybatis.version>3.3.0</mybatis.version>    <!-- log4j日志文件管理包版本 -->    <slf4j.version>1.7.7</slf4j.version>    <log4j.version>1.2.16</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>    <!-- spring对web的支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-web</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring对ORM映射的支持-->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-oxm</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring事务管理支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-tx</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring对JDBC的支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-jdbc</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring对MVC框架的支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring切面支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-aop</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring上下文支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context-support</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- spring测试支持 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-test</artifactId>      <version>${spring.version}</version>    </dependency>    <!-- AspectJ支持 -->    <dependency>      <groupId>aspectj</groupId>      <artifactId>aspectjrt</artifactId>      <version>1.5.4</version>    </dependency>    <!-- 织入 -->    <dependency>      <groupId>aspectj</groupId>      <artifactId>aspectjweaver</artifactId>      <version>1.5.4</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.3</version>    </dependency>    <!-- 导入Mysql数据库链接jar包 -->    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.30</version>    </dependency>    <!-- oracle连接 -->    <dependency>      <groupId>com.oracle</groupId>      <artifactId>classes12</artifactId>      <version>10.2.0.3.0</version>    </dependency>    <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->    <dependency>      <groupId>commons-dbcp</groupId>      <artifactId>commons-dbcp</artifactId>      <version>1.4</version>    </dependency>    <!-- JSTL标签类 -->    <dependency>      <groupId>jstl</groupId>      <artifactId>jstl</artifactId>      <version>1.2</version>    </dependency>    <!-- 日志文件管理包 -->    <!-- log start -->    <dependency>      <groupId>commons-logging</groupId>      <artifactId>commons-logging</artifactId>      <version>1.2</version>    </dependency>    <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>    <!-- 导入java ee jar 包 -->    <dependency>      <groupId>javax</groupId>      <artifactId>javaee-api</artifactId>      <version>7.0</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>com.fasterxml.jackson.core</groupId>      <artifactId>jackson-annotations</artifactId>      <version>2.5.1</version>    </dependency>    <dependency>      <groupId>com.fasterxml.jackson.core</groupId>      <artifactId>jackson-databind</artifactId>      <version>2.5.1</version>    </dependency>    <dependency>      <groupId>com.fasterxml.jackson.core</groupId>      <artifactId>jackson-core</artifactId>      <version>2.5.1</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>com.sea</finalName>  </build></project>
 

 
 
 
 
 

 
 
时间: 2024-10-13 16:23:09

Spring+Mybatis+Maven的相关文章

Idea SpringMVC+Spring+MyBatis+Maven调整【转】

Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetype,然后选中下方列表中的webapp,然后点击Next 在GroupId和ArtifactId中填入指定内容,点击Next 直接点Next 输入项目名称,Finish Idea会自动开始下载所依赖的包,等待其完成. 项目结构 项目刚建好的时候是没有这些文件的,所以自己手动创建缺少的文件夹(包) 创建完后

springmvc+spring+mybatis+maven项目构建

1.首先在myeclipse10中安装maven的插件,将插件放入D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropins\maven中, 2. 新建文件:maven.link填入如下内容:path=D:/Program Files (x86)/myEclipse10/MyEclipse Blue Edition 10/dropins/maven 3.重启myeclipse插件安装成功. 4.在myeclipse10

Spring+Mybatis+Maven 整合配置

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans default-autowire="byName" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:sche

Idea SpringMVC+Spring+MyBatis+Maven+Tomcat调整

Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetype,然后选中下方列表中的webapp,然后点击Next 在GroupId和ArtifactId中填入指定内容,点击Next 直接点Next 输入项目名称,Finish Idea会自动开始下载所依赖的包,等待其完成. 项目结构 项目刚建好的时候是没有这些文件的,所以自己手动创建缺少的文件夹(包) 创建完后

SpringMVC+Spring+Mybatis+Maven整合-使用的工具

STS(Spring Tool Suite):是免费的一款基于Eclipse的Spring开发的定制版IDE,其官方下载地址为 http://www.springsource.org/downloads/sts-ggts 我选择的是现在最新的4.3.1版本,64位. 安装自不必细说. Tomcat:自己百度 JDK:自己谷歌 Mybatis generator:代码生成工具,下一篇中将详细介绍使用 http://code.google.com/p/mybatis/wiki/Downloads?t

单工程搭建springmvc+spring+mybatis(maven,idea)

单工程搭建springmvc+spring+mybatis(maven,idea) 1.pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven

idea+springmvc+spring+mybatis+maven整合返回json数据webapi

首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring-test, spring-orm, commons-dbcp, mybatis, mybatis-spring, mysql-connector-java, commons-fileupload, jstl,jackson-core, jackson-databind, jackson-mappe

Spring+Mybatis+Maven+MySql搭建实例

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例,文章写得很详细,有代码有图片,最后也带有运行的效果. 一.准备工作 1. 首先创建一个表: CREATE TABLE `t_user` ( `USER_ID` int(11) NOT NULL AUTO_INCREMENT, `USER_NAME` char(30) NOT NULL, `USER

大型分布式项目项目实战Springmvc+Spring+Mybatis+Maven+CMS+Redis+Solr+Linux+Nginx+单点登录、分布式缓存、负载均衡视频课程

* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布 式项目实战视频教程 视频课程包含: 高级Java架构师包含:Spring boot.Spring  cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat