栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis2---基本搭建(ssm版)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis2---基本搭建(ssm版)

mybatis2----基本搭建(ssm版) 1.创建项目

1.搭建好环境后,打开idea,点击New - Project

2.找到Maven一栏,因为要搭建的SpringMvc项目,所以选择webapp模板

3.填写好GroupId和ArtifactId后,一步步next,最后finish完成

4.创建成功后,可以看到项目是这样的目录结构

2.首先搭建spring整合springmvc 1.添加maven依赖

  4.0.0
  com.huajia
  ssm_mybatis_demo
  war
  1.0-SNAPSHOT
  ssm_mybatis_demo Maven Webapp
  http://maven.apache.org
  

    

    
    
    
      org.projectlombok
      lombok
      1.16.20
    

    
    
      org.springframework
      spring-core
      4.3.5.RELEASE
    

    
    
      org.springframework
      spring-aop
      4.3.5.RELEASE
    

    
    
      org.springframework
      spring-tx
      4.3.5.RELEASE
    
      
    
      org.springframework
      spring-context
      4.3.5.RELEASE
    

    
      org.springframework
      spring-context-support
      4.3.5.RELEASE
    

    
      org.springframework
      spring-expression
      4.3.5.RELEASE
    
    
    
      org.springframework
      spring-web
      4.3.1.RELEASE
    

    
    
      org.springframework
      spring-webmvc
      4.3.1.RELEASE
    

    
    
      jstl
      jstl
      1.2
    

    
      commons-lang
      commons-lang
      2.6
    

    
    
      com.alibaba
      fastjson
      1.2.12
    
    
    
    
      com.google.code.gson
      gson
      2.6.2
    

  


2.手动添加java和resources文件夹

1.首先分别在main目录下创建java和resources文件夹

2.选择java文件夹,并右键选择“Sources Root”,将文件夹转换

3.选择resources文件夹,并右键选择“Resources Root”,将文件夹转换

3.添加controller,entity,service包,并创建相应的类

1.创建entity包并创建UserTab类

1)UserTab.java

import lombok.Data;

@Data
public class UserTab {

    private Integer userId;

    private String userAccount;

    private String userName;

    private String password;
}

2.创建service包并创建IUserTabService,UserTabServiceImpl类

1)IUserTabService.java

import com.huajia.entity.UserTab;
import java.util.List;

public interface IUserTabService {

    List getUserList();

}

2)UserTabServiceImpl.java

import com.huajia.entity.UserTab;
import com.huajia.service.IUserTabService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserTabServiceImpl implements IUserTabService {

    public List getUserList() {
        List list = new ArrayList<>();
        UserTab userTab = new UserTab();
        userTab.setUserAccount("admin");
        userTab.setPassword("123");
        list.add(userTab);
        return list;
    }
}

3.创建controller包并创建

1)UserTabController.java

import com.huajia.entity.UserTab;
import com.huajia.service.IUserTabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/userTab")
public class UserTabController {
    @Autowired
    private IUserTabService userTabService;

    @GetMapping("/getUserList")
    @ResponseBody
    public List getUserList(){
        return userTabService.getUserList();
    }
}
4.配置Spring的核心配置文件applicationContext.xml

该文件放在resources目录下,是spring的配置,文件内容如下:




    
    
    
    
    


5.配置springMvc的配置文件spring-mvc.xml

该文件放在resources目录下,是springMvc的配置,文件内容如下:




    
    
    

    
    
    
    
        
        	
        	
    

6.配置web.xml

该文件不是放在resources,而是webapp的WEB-INF文件夹下,文件内容如下:



  Archetype Created Web Application

  
  
    /index.jsp
  

  
  
  
  
  
  
  
    spring
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    
    1
  

  
    spring
    /
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
  
    org.springframework.web.context.ContextLoaderListener
  


7.整体代码架构

8.运行

1.点击idea右上角的 Edit Configurations…

2.选择Tomcat Server - Local

3.编辑好项目的启动信息,包括项目名,jdk版本,tomcat以及端口

4.选择Deployment,添加Atifact,选择第二项,否则Tomcat运行会报错


5.保存后,启动项目,浏览器会自动打开首页,即启动成功了

6.访问接口,看下成功与否

接口地址:http://localhost:8080/ssm_mybatis_demo_war_exploded/userTab/getUserList

3.spring整合mybatis 1.添加mybatis相关依赖

  4.0.0
  com.huajia
  ssm_mybatis_demo
  war
  1.0-SNAPSHOT
  ssm_mybatis_demo Maven Webapp
  http://maven.apache.org
  

    

    
    
    
      org.projectlombok
      lombok
      1.16.20
    

    
    
      org.springframework
      spring-core
      4.3.5.RELEASE
    

    
    
      org.springframework
      spring-aop
      4.3.5.RELEASE
    

    
    
      org.springframework
      spring-tx
      4.3.5.RELEASE
    
    
    
      org.springframework
      spring-context
      4.3.5.RELEASE
    

    
      org.springframework
      spring-context-support
      4.3.5.RELEASE
    

    
      org.springframework
      spring-expression
      4.3.5.RELEASE
    
    
    
      org.springframework
      spring-web
      4.3.1.RELEASE
    

    
    
      org.springframework
      spring-webmvc
      4.3.1.RELEASE
    
    
    
    
      jstl
      jstl
      1.2
    

    
      commons-lang
      commons-lang
      2.6
    

    
    
      com.alibaba
      fastjson
      1.2.12
    

    
    
      com.google.code.gson
      gson
      2.6.2
    

    
    
      org.mybatis
      mybatis
      3.3.0
    

    
    
      org.mybatis
      mybatis-spring
      1.2.3
    

    
    
      org.springframework
      spring-jdbc
      4.3.5.RELEASE
    

    
    
      com.alibaba
      druid
      1.1.13
    

    
    
      mysql
      mysql-connector-java
      8.0.25
    

  


2.添加dao包,mapper文件夹,并创建相应的文件

1.创建dao包并创建UserTabMapper类

1)UserTabMapper.java

import com.huajia.entity.UserTab;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface UserTabMapper {

    List getUserList();

}

2.在resources文件夹下创建mapper文件夹,并创建UserTabMapper.xml文件

1)UserTabMapper.xml





    
        
        
    

    
        select user_name,password from user_tab
    


3.修改UserTabServiceImpl.java业务类

1)UserTabServiceImpl.java

import com.huajia.dao.UserTabMapper;
import com.huajia.entity.UserTab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huajia.service.IUserTabService;

import java.util.List;

@Service
public class UserTabServiceImpl implements IUserTabService {

    @Autowired
    private UserTabMapper userTabMapper;

    public List getUserList() {
        return userTabMapper.getUserList();
    }
}

4.添加jdbc配置文件jdbc.properties

该文件放在resources目录下,是数据库的配置,文件内容如下:

#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisdemo?useSSL=false&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
5.添加mybatis配置文件spring-mybatis.xml

该文件放在resources目录下,是mybatis的配置,文件内容如下:




    
        
            
                classpath:jdbc.properties
            
        
    

    
    
        
        
        
        
    

    
    
        
        
    
    
    
        
        
    

    
    
        
    



6.修改Spring的核心配置文件applicationContext.xml



    
    
    
    
    

    
    


7.整体代码架构

8.运行

1.点击idea右上角的 Edit Configurations…

2.选择Tomcat Server - Local

3.编辑好项目的启动信息,包括项目名,jdk版本,tomcat以及端口

4.选择Deployment,添加Atifact,选择第二项,否则Tomcat运行会报错


5.保存后,启动项目,浏览器会自动打开首页,即启动成功了

6.访问接口,看下成功与否

接口地址:http://localhost:8080/ssm_mybatis_demo_war_exploded/userTab/getUserList

4.相关源码

地址:https://gitee.com/lin8081/ssm_mybatis_demo

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号