资讯专栏INFORMATION COLUMN

Spring+SpringMVC+Maven+Mybatis+MySQL+Jetty项目搭建(1)

KoreyLee / 2348人阅读

摘要:接口声明并实现接口声明一个接口新建一个类,并实现接口单元测试单元测试是为了验证第步中接口的方法。中新增类使用实现单元测试指定注入的配置文件使用标准的注释来告诉使用在中新增类文件运行单元测试右键运行结果到此,我们已经搭建了一个基于的项目环境。

本文详细讲述如何搭建一个Spring+SpringMVC+Maven+Mybatis+MySQL项目环境。
eclipse、maven 及 mysql的安装和配置在此不赘述,可参考这里。
本文demo源码可参考这里。
本文demo所用 Eclipse Java EE IDE 版本信息:

</>复制代码

  1. Eclipse Java EE IDE for Web Developers.
  2. Version: Neon.3 Release (4.6.3)
  3. Build id: 20170314-1500
  4. (c) Copyright Eclipse contributors and others 2000, 2017. All rights reserved. Eclipse and the Eclipse logo are trademarks of the Eclipse Foundation, Inc., https://www.eclipse.org/. The Eclipse logo cannot be altered without Eclipse"s permission. Eclipse logos are provided for use under the Eclipse logo and trademark guidelines, https://www.eclipse.org/logotm/. Oracle and Java are trademarks or registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
  5. This product includes software developed by other open source projects including the Apache Software Foundation, https://www.apache.org/.

Maven 配置settings.xml(参考这里,这是一个非常好的 maven 配置,资源更新速度特别快...):

</>复制代码

  1. E:RepositoriesMaven
  2. org.mortbay.jetty
  3. releases
  4. ali
  5. ali
  6. Snapshots
  7. ali
  8. ali
  9. nexus
  10. *
  11. http://maven.aliyun.com/nexus/content/groups/public/
  12. nexus-public-snapshots
  13. public-snapshots
  14. http://maven.aliyun.com/nexus/content/repositories/snapshots/
  15. development
  16. central
  17. http://central
  18. truealways
  19. truealways
  20. central
  21. http://central
  22. truealways
  23. truealways
  24. public-snapshots
  25. public-snapshots
  26. http://public-snapshots
  27. false
  28. truealways
  29. public-snapshots
  30. http://public-snapshots
  31. false
  32. truealways
  33. development
  34. public-snapshots

开始搭建项目:

1.mysql 数据库及表准备:

</>复制代码

  1. CREATE TABLE `T_USER` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `user_type` tinyint(4) DEFAULT NULL COMMENT "0、系统管理,1、业务管理员,2、普通操作员",
  4. `mobile` varchar(255) DEFAULT NULL COMMENT "管理员手机号",
  5. `is_biz_operator` tinyint(4) DEFAULT NULL COMMENT "是否业务操作员 0: 不是 1:是的",
  6. `is_sys_operator` tinyint(4) DEFAULT NULL COMMENT "是否系统操作员 0: 不是 1:是的",
  7. `login_name` varchar(255) NOT NULL COMMENT "员工后台管理账号",
  8. `real_name` varchar(100) DEFAULT NULL COMMENT "用户名",
  9. `password` varchar(255) NOT NULL COMMENT "员工后台管理密码",
  10. `status` tinyint(4) NOT NULL COMMENT "是否有效 0:无效 1:有效",
  11. `creator` bigint(20) DEFAULT NULL COMMENT "创建者",
  12. `updator` bigint(20) DEFAULT NULL COMMENT "更新者",
  13. `create_date` datetime DEFAULT NULL COMMENT "创建日期",
  14. `update_date` datetime DEFAULT NULL COMMENT "更新日期",
  15. `deleted` tinyint(4) NOT NULL DEFAULT "0" COMMENT "0:没删除 1:删除",
  16. PRIMARY KEY (`id`),
  17. UNIQUE KEY `unique_login_name` (`login_name`) USING BTREE
  18. ) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;

2.maven 工程创建



工程新建完成后,工程的目录结构如下:

3.编辑 pom.xml 文件,添加工程的包依赖

</>复制代码

  1. 4.0.0
  2. com.hy
  3. ssm001
  4. 0.0.1-SNAPSHOT
  5. war
  6. 3.2.8.RELEASE
  7. 1.6.6
  8. 1.2.12
  9. 4.10
  10. 3.2.1
  11. org.springframework
  12. spring-core
  13. ${spring.version}
  14. org.springframework
  15. spring-webmvc
  16. ${spring.version}
  17. org.springframework
  18. spring-context
  19. ${spring.version}
  20. org.springframework
  21. spring-context-support
  22. ${spring.version}
  23. org.springframework
  24. spring-aop
  25. ${spring.version}
  26. org.springframework
  27. spring-aspects
  28. ${spring.version}
  29. org.springframework
  30. spring-tx
  31. ${spring.version}
  32. org.springframework
  33. spring-jdbc
  34. ${spring.version}
  35. org.springframework
  36. spring-web
  37. ${spring.version}
  38. junit
  39. junit
  40. ${junit.version}
  41. test
  42. log4j
  43. log4j
  44. ${log4j.version}
  45. org.slf4j
  46. slf4j-api
  47. ${slf4j.version}
  48. org.slf4j
  49. slf4j-log4j12
  50. ${slf4j.version}
  51. org.springframework
  52. spring-test
  53. ${spring.version}
  54. test
  55. org.mybatis
  56. mybatis
  57. ${mybatis.version}
  58. org.mybatis
  59. mybatis-spring
  60. 1.2.0
  61. mysql
  62. mysql-connector-java
  63. 5.1.29

4.配置文件初始化。
分别初始化mybatis配置文件、数据库配置文件、Spring配置文件。

mybatis配置文件 mybatis-config.xml:

</>复制代码

数据库配置文件 jdbc.properties:

</>复制代码

  1. jdbc_driverClassName=com.mysql.jdbc.Driver
  2. jdbc_url=jdbc:mysql://192.168.8.*:3306/db_user
  3. jdbc_username=*
  4. jdbc_password=*

spring配置文件 application.xml:

</>复制代码

  1. classpath:properties/*.properties
  2. ${jdbc_driverClassName}
  3. ${jdbc_url}
  4. ${jdbc_username}
  5. ${jdbc_password}

5.mybatis 反向生成代码
MyBatis属于一种半自动的ORM框架,由于手写Mapping映射文件很容易出错,所以可利用 MyBatis生成器 自动生成实体类、DAO接口和Mapping映射文件。有两种方式:

第一种方式:安装 eclipse mybatis generator 插件,使用该插件实现代码生成。
可参考这里。
第二种方式:直接用 mybatis-generator.jar 包的命令方式。
相关的jar包及命令可查看本demo源码:

在本文的 demo 中使用了第二种方式。mybatis generator 配置文件generatorConfig.xm内容如下:

</>复制代码

</>复制代码

  1. cd:G:javaSpacessm001srcmain
  2. esourcesmybatis-generator
  3. java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

执行以上命令后,工程目录中生成了与 mybatis 相关的代码,主要有实体类、DAO接口和Mapping映射文件。

6.接口声明并实现接口
声明一个UserService接口:

</>复制代码

  1. package com.hy.service;
  2. import com.hy.domain.User;
  3. public interface UserService {
  4. User getUserById(Long id);
  5. }

新建一个类 UserServiceImpl,并实现 UserService 接口 :

</>复制代码

  1. package com.hy.service.impl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import com.hy.dao.UserMapper;
  5. import com.hy.domain.User;
  6. import com.hy.service.UserService;
  7. @Service
  8. public class UserServiceImpl implements UserService{
  9. @Autowired
  10. private UserMapper userMapper;
  11. public User getUserById(Long id) {
  12. return userMapper.selectByPrimaryKey(id);
  13. }
  14. }

7.单元测试
单元测试是为了验证第6步中 UserService 接口的 getUserById 方法。
com.hy.baseTest 中新增类 SpringTestCase.java,使用 SpringJUnit4ClassRunner 实现单元测试:

</>复制代码

  1. package com.hy.baseTest;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.test.context.ContextConfiguration;
  4. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. //指定bean注入的配置文件
  7. @ContextConfiguration(locations = { "classpath:application.xml" })
  8. //使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
  9. @RunWith(SpringJUnit4ClassRunner.class)
  10. public class SpringTestCase extends AbstractJUnit4SpringContextTests {
  11. }

在 com.hy.test 中新增 UserServiceTest 类文件:

</>复制代码

  1. package com.hy.test;
  2. import org.junit.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import com.hy.baseTest.SpringTestCase;
  5. import com.hy.domain.User;
  6. import com.hy.service.UserService;
  7. public class UserServiceTest extends SpringTestCase {
  8. @Autowired
  9. private UserService userService;
  10. @Test
  11. public void selectUserByIdTest(){
  12. User user = userService.getUserById((long) 1);
  13. System.out.println("userLoginName:" + user.getLoginName());
  14. }
  15. }

运行单元测试:UserServiceTest 右键 Run As –>Junit Test, 运行结果:

</>复制代码

  1. log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
  2. log4j:WARN Please initialize the log4j system properly.
  3. userLoginName:admin

到此,我们已经搭建了一个基于 Spring+Maven+Mybatis+Mysql 的项目环境。文中使用了很多Spring相关的技术,如自动注入注解@Service、@Autowired,以及Spring配置文件,这些知识可参考:
http://www.cnblogs.com/szlbm/...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67226.html

相关文章

  • 【Java】基于Maven搭建Spring+SpringMVC+Mybatis框架

    摘要:关于的配置,可以参考这篇文章的第一个小节配置模板引擎搭什么搭直接用脚手架不行吗下载就能用下载就能用下载就能用码云咳咳,开个玩笑,如果本着学习态度的话,那就慢慢啃吧搭建空的项目使用搭建基本的空项目填写和,,选择项目的地址,在新的窗口打开最 关于springMVC的配置,可以参考这篇文章的第一个小节:【java】intellij idea SpringMVC 配置FreeMarker模板引...

    edagarli 评论0 收藏0
  • 搭建一个SSM项目

    摘要:一新建一个工程为什么要用搭建项目可以对项目依赖的包进行管理,需要的包只需要到仓库里面去拿到版本信息复制到文件即可。 一 新建一个Maven工程 1.1 为什么要用Maven搭建项目? Maven可以对项目依赖的jar包进行管理,需要的jar包只需要到Maven仓库里面去拿到版本信息复制到pom.xml文件即可。同时,它也能对项目进行编译、测试、打包等功能。 1.2 新建一个Ma...

    edgardeng 评论0 收藏0
  • 使用IDEA基于Maven搭建多模块聚合工程(springmvc+spring+mybatis整合)

    摘要:最后运行,如下图所示,就说明跑通了总结之前看别人的博客,有选择项,自己弄死活跑不通。选择那项,啥都不选选择那项。还要注意打包方式,,, 一.工程目录 下面是搭建之后的目录showImg(https://segmentfault.com/img/remote/1460000015755454?w=407&h=467); 先看一下目录关系 taotao-parent(父工程管理jar包的版...

    szysky 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<