基于XML连接MySQL

package com.it.domain;import java.io.Serializable;/*** @Author: 东方老赢* @Date: 2020/4/2 11:07** 账户的实体类*/
public class Account implements Serializable {private Integer id;private String name;private Float money;public Account(Integer id, String name, Float money) {this.id = id;this.name = name;this.money = money;}public Account(String name, Float money) {this.name = name;this.money = money;}public Account(){}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;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
package com.it.dao;import com.it.domain.Account;import java.util.List;/*** @Author: 东方老赢* @Date: 2020/4/2 11:20*/
public interface IAccountDao {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccount(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param accountId*/void deleteAccount(Integer accountId);
}
package com.it.dao.impl;import com.it.dao.IAccountDao;
import com.it.domain.Account;
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;/*** @Author: 东方老赢* @Date: 2020/4/2 11:19*/
public class AccountDaoImpl implements IAccountDao{private QueryRunner runner;public AccountDaoImpl(QueryRunner runner) {this.runner = runner;}public AccountDaoImpl() {}public List<Account> findAllAccount() {try {return runner.query("select * from account",new BeanListHandler<Account>(Account.class));} catch (Exception e) {throw new RuntimeException(e);}}public Account findAccount(Integer accountId) {try {return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);} catch (Exception e) {throw new RuntimeException(e);}}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);}}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);}}public void deleteAccount(Integer accountId) {try {runner.update("delete from account where id = ?",accountId);} catch (Exception e) {throw new RuntimeException(e);}}public void setRunner(QueryRunner runner) {this.runner = runner;}public QueryRunner getRunner() {return runner;}
}
package com.it.service;import com.it.domain.Account;import java.util.List;/*** @Author: 东方老赢* @Date: 2020/4/2 11:06** 账户的业务层接口*/
public interface IAccountService {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccount(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param accountId*/void deleteAccount(Integer accountId);
}
package com.it.service.impl;import com.it.dao.IAccountDao;
import com.it.domain.Account;
import com.it.service.IAccountService;import java.util.List;/*** @Author: 东方老赢* @Date: 2020/4/2 11:18** 账户的业务层*/
public class AccountServiceImpl implements IAccountService {private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}public List<Account> findAllAccount() {return accountDao.findAllAccount();}public Account findAccount(Integer accountId) {return accountDao.findAccount(accountId);}public void saveAccount(Account account) {accountDao.saveAccount(account);}public void updateAccount(Account account) {accountDao.updateAccount(account);}public void deleteAccount(Integer accountId) {accountDao.deleteAccount(accountId);}
}
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--1.配置Service对象--><bean id="accountService" class="com.it.service.impl.AccountServiceImpl"><!--注入dao--><property name="accountDao" ref="accountDao"></property></bean><!--2.配置Dao对象--><bean id="accountDao" class="com.it.dao.impl.AccountDaoImpl"><!--注入QueryRunner--><property name="runner" ref="runner"></property></bean><!--3.配置Queryrunner对象:多例,防止线程冲突--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner"  scope="prototype"><!--注入数据源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!--4.配置数据源--><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/spring"></property><property name="user" value="root"></property><property name="password" value="123456"></property></bean></beans>
<?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.it</groupId><artifactId>day02_eesy_03account _annoioc</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.0.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.6</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.36</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.1</version></dependency><dependency><groupId>org.junit</groupId><artifactId>com.springsource.org.junit</artifactId><version>4.7.0</version><scope>test</scope></dependency></dependencies></project>

基于注解连接MySQL

这里只展示与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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.it"/><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/spring"></property><property name="user" value="root"></property><property name="password" value="123456"></property></bean>
</beans>
package com.it.dao;import com.it.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;import java.util.List;/*** @Author: 东方老赢* @Date: 2020/4/3 10:02*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao{@Autowiredprivate QueryRunner runner;//    public List<Account> findAll() {//        try {//            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
//        } catch (Exception e) {//           throw new RuntimeException(e);
//        }
//    }public AccountDaoImpl(){}public List<Account> findAll() {try {return runner.query("select * from account",new BeanListHandler<Account>(Account.class));} catch (Exception e) {throw new RuntimeException(e);}}public Account find(Integer accountId) {try {return runner.query("select * from account where id =?",new BeanHandler<Account>(Account.class),accountId);} catch (Exception e) {throw new RuntimeException(e);}}public void save(Account account) {try {runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());} catch (Exception e) {throw new RuntimeException(e);}}public void delete(Integer accountId) {try {runner.update("delete from account where id = ?",accountId);} catch (Exception e) {throw new RuntimeException(e);}}public void update(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);}}}

package com.it.service;import com.it.dao.AccountDaoImpl;
import com.it.dao.IAccountDao;
import com.it.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** @Author: 东方老赢* @Date: 2020/4/3 10:07*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService {@AutowiredIAccountDao accountDao;public List<Account> findAll() {return accountDao.findAll();}public Account find(Integer accountId) {return accountDao.find(accountId);}public void save(Account account) {accountDao.save(account);}public void delete(Integer accountId) {accountDao.delete(accountId);}public void update(Account account) {accountDao.update(account);}}

Spring基于XMLMysql | 注解Mysql的简单IOC案例相关推荐

  1. 【Spring】spring基于纯注解的声明式事务控制

    结构 去掉bean.xml config JdbcConfig package com.itheima.config;import org.springframework.beans.factory. ...

  2. spring基于纯注解的声明式事务控制

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  3. Quartz定时任务基于ssm注解方式的简单使用

    Maven配置 <!-- quartz监控 --><dependency><groupId>org.quartz-scheduler</groupId> ...

  4. 基于jsp+ssm+mysql实现简单的物流快递管理系统

    <物流快递管理系统>该项目采用技术:spring+springMVC+mybaits+EasyUI+jQuery+Ajax等相关技术,采用MVC模式,项目含有源码.文档.配套开发软件.软件 ...

  5. php mysql crud demo_基于php和mysql的简单的dao类实现crud操作功能_PHP教程

    代码如下: public function SimpleDao() { if ($this->_con == null) { $this->_con = @mysql_connect(&q ...

  6. 基于JAVA+SpringMVC+MYSQL的简单企业人事管理系统

    项目功能: 系统包括用户登录注册,员工列表展示,修改员工信息,添加员工,删除员工,按员工姓名查询员工,退出登录 页面效果:

  7. 基于PHP和mysql的简单学生成绩管理系统

    本系统主要架构图如上图所示.PS:register_check.php的自动注册代码写在check.php里了. 使用数据库的两张表,user_info用户信息表和stu_info学生成绩信息表. 提 ...

  8. php与MYSQL制作学生成绩系统,基于PHP和mysql的简单学生成绩管理系统

    本系统主要架构图如上图所示.PS:register_check.php的自动注册代码写在check.php里了. 使用数据库的两张表,user_info用户信息表和stu_info学生成绩信息表. 提 ...

  9. nari基于osp平台的后端简单curd案例

    本片主要描述了基于osp平台的简单的增删改查的小案例.便于记忆. 以table表格的前后端拿数据为例子! 在table中展示数据 @Controller @RequestMapping("/ ...

最新文章

  1. iptables防DDOS***和CC***设置
  2. 子站间 携带cookie_JavaScript cookie 不同子域名之间共享
  3. Swift - 做一个简单的无线U盘(手机端Http服务器搭建)
  4. oracle构造过程实例
  5. 推荐算法炼丹笔记:推荐系统采样评估指标及线上线下一致性问题
  6. [源码学习]调试Razor从哪里开始
  7. 手机局域网html,手机遥控电脑开机神器!局域网唤醒App
  8. ASP.net控件开发系列之(一、二)
  9. TortoiseGit客户端安装及使用(上传代码到git@osc
  10. 打造Vim作为前端IDE
  11. 网页扫雷html css js,GitHub - zsr204/Sweep: js + html + css 实现一个简单的扫雷~~ 附加 难度选择 计时 计雷数 开始 重新开始 功能...
  12. SQL Server 按间隔时间查询记录
  13. 为什么装完计算机系统后进不去,电脑重新装完系统后开机后就这个样子,一直进不去是为什么?...
  14. 宝塔绑定域名访问不了_千字长文教你使用 宝塔面板 快速搭建网站
  15. CDN 加速 OSS 常见问题及处理思路
  16. 这几种神级性能优化手段,你用过几个?
  17. 好问题:为什么有些大公司技术弱爆了?
  18. 青龙面板JD各大库合集
  19. 在windows上搭建React Native开发环境
  20. 小米手机拦截返回音设置不了_为了让自己的手机更好用,我利用了 MIUI 10 的这些功能...

热门文章

  1. C语言编程————杨辉三角
  2. [译]GitHub应对1.28宕机事故的前前后后
  3. 微信小程序使用三元运算符
  4. OpenAI生成二次元美女【辣眼睛慎入】
  5. 秋招Java岗,心态大崩,今年的面试真的有必要这么卷吗?
  6. 2011年国庆2天厦门到平潭岛湄洲岛自驾游
  7. 再说System Verilog 与 Verilog 的关系
  8. 鲁菜泰斗同和居 日坛新店用上便民新科技
  9. DeepTraLog: Trace-Log Combined Microservice AnomalyDetection through Graph-based Deep Learning
  10. 产品经理如何营销自己