idea搭建SSM项目这一篇就够了 |
您所在的位置:网站首页 › 查理王小猎犬能长多大 › idea搭建SSM项目这一篇就够了 |
目录 1. 创建Maven项目 1.1 创建一个新的项目 (请先连接好网络) 1.2 选择maven --> 选择你的jdk版本 --> 勾选Create from archetype 1.3 随便选中一条输入webapp进行模糊查询然后选中下列我选中的内容(重要!不可错) 1.4 填写你的项目名 --> 选择项目保存到你的电脑地址 --> 随便写(最好用两个单词中间用.隔开) --> 和项目名保持一致 1.5 选择你的maven安装包地址 --> 查看settings.xml文件是否在idea显示的地址 -->查看你的下载资源本地仓库是否在idea显示的地址(如果有误或自己整改过都要进行修改和idea保持一致) 1.6 正在创建中...请稍后(注意!!!创建maven项目请先连接网络) 1.7 控制台出现(BUILD SUCCESS)代表Maven项目已创建成功 2. 搭建项目相关目录 2.1 选中main文件夹右键 2.2 new一个新的文件夹选中(new --> Directory) 2.3 此操作要操作两遍因为要添加下方图中这两个文件夹(java,resources) 2.4 选中src文件夹右键 2.5 new一个新的文件夹(new --> Directory) 2.6 选中添加下方图中文件夹即可(test\java) 3. 添加pom相关依赖 4. 添加/修改相关配置文件 4.1 jdbc.properties(添加到main文件夹 --> resources文件夹下面) 4.2 log4j2.xml(添加到main文件夹 --> resources文件夹下面) 4.3 mybatis.cfg.xml(添加到main文件夹 --> resources文件夹下面) 4.4 spring.xml(添加到main文件夹 --> resources文件夹下面) 4.5 spring-mybatis.xml(添加到main文件夹 --> resources文件夹下面) 4.6 spring-mvc.xml(添加到main文件夹 --> webapp文件夹 --> WEB-INF文件夹下面) 4.7 修改web.xml相关配置 4.8 generatorConfig.xml(添加到main文件夹 --> resources文件夹下面) 4.8.1 添加Maven自动生成插件 5. 使用junit and Tomcat分别测试是否搭建成功 5.1 junit测试 5.2 Tomcat测试 1. 创建Maven项目 1.1 创建一个新的项目 (请先连接好网络)![]() ![]() ![]() 下方配置文件需要修改: 1. 你的数据库名称 2. 你的数据库登录账号 3. 你的数据库登录密码 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=UTF-8 jdbc.username=你的数据库登录账号 jdbc.password=你的数据库登录密码 4.2 log4j2.xml(添加到main文件夹 --> resources文件夹下面) /root/workspace/lucenedemo/logs /root/workspace/lucenedemo/logs/error /root/workspace/lucenedemo/logs/warn %d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level %logger{36} - %msg%n 4.3 mybatis.cfg.xml(添加到main文件夹 --> resources文件夹下面) 4.4 spring.xml(添加到main文件夹 --> resources文件夹下面) classpath:jdbc.properties 4.5 spring-mybatis.xml(添加到main文件夹 --> resources文件夹下面)下方配置文件需要修改(注!!!需要修改的地方已用注释标注): 4.6 spring-mvc.xml(添加到main文件夹 --> webapp文件夹 --> WEB-INF文件夹下面) 下方配置文件需要修改(注!!!需要修改的地方已用注释标注): 下方配置文件需要修改(注!!!需要修改的地方已用注释标注): 1. 需要修改01(你要映射的.xml文件名) 2. 需要修改02 (中文乱码过滤器配置(已在下方提供代码资源)) 3. 需要修改03 (那里面的.xml文件修改成上方4.6添加的spring-mvc.xml文件名即可) Archetype Created Web Application contextConfigLocation classpath:spring.xml org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener encodingFilter com.jmh.shiro.utils.EncodingFiter true encoding UTF-8 encodingFilter /* SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring-mvc.xml 1 true SpringMVC / EncodingFiterpackage com.jmh.ssm.utils; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; /** * 中文乱码处理 * */ public class EncodingFiter implements Filter { private String encoding = "UTF-8";// 默认字符集 public EncodingFiter() { super(); } public void destroy() { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String method = request.getMethod(); if (method.equalsIgnoreCase("get")) { Map map = (Map) request.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合 for (String[] values : map.values()) { for (int i = 0; i < values.length; i++) { values[i] = new String(values[i].getBytes("ISO-8859-1"), this.encoding); } } } else if (method.equalsIgnoreCase("post")) { request.setCharacterEncoding(this.encoding); } response.setContentType("text/html;charset=" + this.encoding); chain.doFilter(servletRequest, servletResponse); } public void init(FilterConfig filterConfig) throws ServletException { String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集 if (null != s && !s.trim().equals("")) { this.encoding = s.trim(); } } } 4.8 generatorConfig.xml(添加到main文件夹 --> resources文件夹下面)下方配置文件需要修改(注!!!需要修改的地方已用注释标注): 1. 需要修改01 (你的mysql jar包的放置地址路径位置) 2. 需要修改02 (生成实体类的模块位置比如xxx.xxx.xxx.model) 3. 需要修改03 (生成sql映射文件位置) 4. 需要修改04 (生成mapper接口位置) 5. 最后就是table标签需要修改的属性值!一个table标签就是一张表可以这么理解 需要修改的table标签也就两个属性: 5.1 tableName="这里填写的就是你要生成的表名称" 5.2 domainObjectName="这里就是你要生成的表对应的实体类名称" 4.8.1 添加Maven自动生成插件命令如下: mybatis-generator:generate -e 好吧!我还是当个老保姆一步步带你们怎么添加好吧 [表情委屈委屈] 4.8.1.1 4.8.1.2 点击+号 4.8.1.3 选择Maven 4.8.1.4 注意!复制命令的时候不要多复制空 4.8.1.5 4.8.1.6 ok!下方就是生成成功啦 如果你没有像图中这个小企鹅的话你也可以忽略这个作用就是方便我们查看sql映射文件的代码...如果你想要这个功能的话下载Mybatis插件“ free mybatis plugins”如果没有这个插件的话就是版本问题就下载Mybatis插件‘’ MybatisX ” ok!ssm项目基本就搭建好了然后下方我们就测试一下吧 5. 使用junit and Tomcat分别测试是否搭建成功 5.1 junit测试5.1.1 需要在Mapper层接口添加@Repository注解 @Repository 将DAO类声明为Bean3 5.1.2 Service层代码 需在service层接口添加@Transactional注解 可直接复制Mapper接口粘贴即可需修改@注解@Transactional 注解配置事务,个人感觉写XML配置声明式事务更加简单 @Transactional(readOnly = true) 在方法前面加上就代表是只读事务 package com.jmh.ssm.service; import com.jmh.ssm.model.User; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Transactional public interface IUserService { int deleteByPrimaryKey(Integer userid); int insert(User record); int insertSelective(User record); @Transactional(readOnly = true) User selectByPrimaryKey(Integer userid); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }5.1.3 Service.impl层代码 需在Service.impl层接口实现类添加@Service() 注解 此时Service实现类需要依赖Mapper层接口 我们需要注入Mapper层接口添加@Resource注解@Service() 通常作用在业务层 @Resource的装配顺序: 1、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配 2、指定了name或者type则根据指定的类型去匹配bean 3、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错 package com.jmh.ssm.service.impl; import com.jmh.ssm.mapper.UserMapper; import com.jmh.ssm.model.User; import com.jmh.ssm.service.IUserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserServiceImpl implements IUserService { //注入 @Resource private UserMapper userMapper; @Override public int deleteByPrimaryKey(Integer userid) { return 0; } @Override public int insert(User record) { return 0; } @Override public int insertSelective(User record) { return 0; } @Override public User selectByPrimaryKey(Integer userid) { return userMapper.selectByPrimaryKey(userid); } @Override public int updateByPrimaryKeySelective(User record) { return 0; } @Override public int updateByPrimaryKey(User record) { return 0; } }5.1.4 使用junit单元测试 创建BaseTestCat类创建BaseTestCat类需添加以下两个注解: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring.xml"})spring.xml文件对应你要映射的.xml文件名 package com.jmh.mybatis.service.impl; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring.xml"}) public class BaseTestCat { @Before public void setUp(){ } }创建junit测试类需继承上方BaseTestCat类 重写BaseTestCat类里面的方法package com.jmh.ssm.service.impl; import com.jmh.ssm.model.User; import com.jmh.ssm.service.IUserService; import org.junit.Test; import javax.annotation.Resource; import static org.junit.Assert.*; public class UserServiceImplTest extends BaseTestCat{ //注入 @Resource private IUserService iUserService; @Override public void setUp() { super.setUp(); } @Test public void deleteByPrimaryKey() { } @Test public void insert() { } @Test public void insertSelective() { } @Test public void selectByPrimaryKey() { User user = iUserService.selectByPrimaryKey(1); System.out.println(user); } @Test public void updateByPrimaryKeySelective() { } @Test public void updateByPrimaryKey() { } }随便测试一个方法成功就是成功!失败就是失败!
5.2.1 前端代码(在WEB-INF下面创建一个jsp文件夹 在jsp文件夹里面创建一个index.jsp页面) 搭建SSM项目 搭建SSM项目成功!5.2.2 Controller层代码 package com.jmh.ssm.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Tomcat测试 */ @Controller public class SsmController { @RequestMapping("/")//请求路径:当前/是打开服务器自动请求的第一个页面 public String toIndex(){ return "index";//跳转视图名称 } }5.2.3 搭建Tomcat服务器 嘿嘿!现在最关键的时候到啦看看能不能第一时间访问index.jsp页面啦 ok!访问成功 测试完毕 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |