SSM+Maven入门实例教程详解(Java)

1、开篇
最近在学习Java,所以在继续撰写LeetCode算法系列后,会推出Java学习系列,请大家支持。
2、环境准备
我使用环境如下:
Mysql WorkBench 8.0 CE + IJ2019.1.3 + Maven3.6.1
3.DEMO实例
(1)项目架构
在这里插入图片描述
(2)项目文件具体代码
IAccountDao代码:

package com.itheima.dao;


import com.itheima.domian.Account;

import java.sql.SQLException;
import java.util.List;

public interface IAccountDao {

    List<Account> findAllAccount() throws SQLException;

    Account findAccountById(Integer accountId);

    void saveAccount(Account account);

    void updateAccount(Account account);

    void deleteAccount(Integer accountId);
}

AccountDaoImpl代码:

package com.itheima.dao.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.domian.Account;
import com.itheima.service.IAccountService;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AccountDaoImpl implements IAccountDao {

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    private QueryRunner runner;


    @Override
    public List<Account> findAllAccount()  {
        try
        {
            return runner.query("Select * from account",new BeanListHandler<Account>(Account.class));
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Account findAccountById(Integer accountId) {
        try
        {
            return runner.query("Select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try
        {
            runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccount(Account account) {
        try
        {
            runner.update("update account set name = ?,money = ? where id = ?",account.getName(),account.getMoney(),account.getId());
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAccount(Integer accountId) {
        try
        {
            runner.update("delete account where id = ?",accountId);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
}

domain—Account代码:

package com.itheima.domian;

import java.io.Serializable;

/**
 * 账户的实体类
 */
public class Account  implements Serializable {
    private Integer id;
    private String name;

    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 Float getMoney() {
        return money;
    }

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

    private Float money;

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

IAccountService代码:

package com.itheima.service;

import com.itheima.domian.Account;

import java.sql.SQLException;
import java.util.List;

/**
 * 账户的业务层接口
 */

public interface IAccountService {

    List<Account> findAllAccount() throws SQLException;

    Account findAccountById(Integer accountId);

    void saveAccount(Account account);

    void updateAccount(Account account);

    void deleteAccount(Integer accountId);
}

AccountServiceImpl代码:

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.domian.Account;
import com.itheima.service.IAccountService;

import java.sql.SQLException;
import java.util.List;

public class AccountServiceImpl implements IAccountService {


    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    private IAccountDao accountDao;

    @Override
    public List<Account> findAllAccount() throws SQLException {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {
         accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
         accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(Integer accountId) {
          accountDao.deleteAccount(accountId);
    }
}

beam.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "accountService" class = "com.itheima.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <bean id = "accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name = "runner" ref="runner">
        </property>
    </bean>


    <bean id = "runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
            <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>
    <bean id = "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/world?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT"></property>
        <property name="user" value="root"></property>
        <property name="password" value="111111"></property>
    </bean>

</beans>

说明:Mysql8.0版本连接需要加上:useUnicode=true&characterEncoding=utf8;调整时区需要加上:serverTimezone=GMT;不然会报错。采用Mysql8.0以下版本可以忽略。
AccountServiceTest代码:

package com.itheima.test;

import com.itheima.domian.Account;
import com.itheima.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;
import java.util.List;

public class AccountServiceTest {
    @Test
    public void testFindAll() throws SQLException {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beam.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        List<Account> accounts = as.findAllAccount();
        for(Account account : accounts)
        {
            System.out.println(account);
        }

    }

    @Test
    public void testFindOne() {

    }
}

pom.xml代码:

<?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>day02_eesy_20account</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>orp.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3、总结
本实例是采用QueryRunner 对数据库进行存取,也可采用AutoMapping的方式进行映射,原理都是一样的,能获取数据库实体即可;
整个工程简介明了,采用DDD模式,XML文件与数据库建立连接,通过AccountDaoImpl获取所需要的实体类Account,Service类定义接口,ServiceImpl根据逻辑对AccountDaoImpl获取到的Account类进行组装(本实例没有在进行更为复杂的组装),此处Service可供Controller或者前端调用(本实例暂时没有),相当于接口。
在这里插入图片描述
以后会定时上传至github,再贴出地址!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值