SSM/Spring5MVC/Spring5/MyBatis/超详细整合(1)

时间:2022-12-11 08:34:12
掌握ssm整合(SpringMVC+spring+mybatis)

       深圳 -- 桃桃桃先生 -- 各位道友我们互相学习 

[环境准备]

1.创建数据库和表结构


create database ssm;
use ssm;
create table account(
    id int primary key auto_increment,
    name varchar(40),
    money double
)character set utf8 collate utf8_general_ci;

insert into account(name,money) values('zs',1000);
insert into account(name,money) values('ls',1000);
insert into account(name,money) values('ww',1000);


2.创建Maven工程webapp (导入坐标)


<?xml version="1.0" encoding="UTF-8"?>

<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.itheima</groupId>
  <artifactId>SSM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
      <spring.version>5.0.2.RELEASE</spring.version>
      <slf4j.version>1.6.6</slf4j.version>
      <log4j.version>1.2.12</log4j.version>
      <mysql.version>5.1.6</mysql.version>
      <mybatis.version>3.4.5</mybatis.version>
  </properties>

    <dependencies>
        <!-- spring 家族-->
        <!--spring核心包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring AOP相关的坐标-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <!--spring AOP 允许开发者定义方法拦截器和切点-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring-web包含http客户端,多文件上传,利用监听器进行ioc容器初始化,提供应用上下文等功能-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--提供MVC和RESTWEB服务实现-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring整合单元测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</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>
        <!--测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</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 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.14</version>
        </dependency>

    </dependencies>

  <build>
      <plugins>
      <!--jdk编译插件-->
  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
  <source>1.8</source>
  <target>1.8</target>
  <encoding>utf-8</encoding>
</configuration>
  </plugin>
      </plugins>
  </build>
</project>



3.实体类pojo->java->bean->列如:Account

package com.itheima.bean;

public class Account {
    private Integer id;
    private String name;
    private double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "ssm{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}



4.业务层接口类->java->service->列如:AccountService(接口)

package com.itheima.service;

import com.itheima.bean.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AccountService{
    /**
     * 查询全部
     * @return
     */
    public List<Account> findAll();

    /**
     * 查询用户
     * @param id
     * @return
     */
    public Account findById(int id);

    /**
     * 保存用户
     * @param account
     */
    public void save(Account account);
}



5.持久层接口类->java->mapper/dao->列如:AccountMapper(接口)

package com.itheima.mapper;

import com.itheima.bean.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AccountMapper {

    /**
     * 查询全部
     * @return
     */
    @Select("select * from account")
    public List<Account> findAll();

    /**
     * 查询用户
     * @param id
     * @return
     */
    @Select("select * account where id=#{id}")
    public Account findById(int id);

    /**
     * 保存用户
     * @param account
     */
    @Insert("insert into account(name,money) values (#{name},#{money})")
    public void save(Account account);

}


6.添加log4j日志系统->resources->log4j.properties

##设置日志记录到控制台的方式
log4j.appender.std=org.apache.log4j.ConsoleAppender
log4j.appender.std.Target=System.err
log4j.appender.std.layout=org.apache.log4j.PatternLayout
log4j.appender.std.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}%5p%c{1}:%L-%m%n

##设置日志记录到文件的方式
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=mylog.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L-%m%n

##日志输出的级别,以及配置记录方案 debug级别 std控制台输出 file文件输出
log4j.rootLogger=debug,std,file

##级别:error > warn > info>debug>trace
##如果设置了某个级别, 只会打印比这个级别高的或者等于这个级别的日志


Spring5MVC
7.控制层->java->web->列如: AccountController

import javax.annotation.Resource;
import java.util.List;

/**
 * 帐号控制器
 */
@Controller
@RequestMapping("/AccountController")
public class AccountController {

    @Resource(name="accountService")
    private AccountService accountService;
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Account> list = accountService.findAll();
        System.out.println(list);
        model.addAttribute("list",list);
        return  "success";
    }
}


------------------字数限制---另外开一篇继续:SSM/Spring5MVC/Spring5/MyBatis/超详细整合(2) 

2 个解决方案

#1


SSM/Spring5MVC/Spring5/MyBatis/超详细整合(1)

第一次发帖不懂  怎么发帖还要积分 

SSM/Spring5MVC/Spring5/MyBatis/超详细整合(1)

#2


  为什么写的时候 没有这个 <br data-filtered="filtered">  预览正常 出来却是一堆这个<br data-filtered="filtered">

#1


SSM/Spring5MVC/Spring5/MyBatis/超详细整合(1)

第一次发帖不懂  怎么发帖还要积分 

SSM/Spring5MVC/Spring5/MyBatis/超详细整合(1)

#2


  为什么写的时候 没有这个 <br data-filtered="filtered">  预览正常 出来却是一堆这个<br data-filtered="filtered">