当前位置: 首页 > news >正文

毕业设计网站建设百度一下官网首页百度一下百度

毕业设计网站建设,百度一下官网首页百度一下百度,网站建设专业的,wordpress 4.4.8文章目录 准备数据pom.xml文件中引用需要的库准备好dao层接口和service层接口和实现类准备好 jdbc.properties 和 user.properties编写Druid的jdbcConfig配置类编写spring的配置类SpringConfig编写Dao层的实现类的逻辑测试类参考文献 准备数据 create database if not exists …

文章目录

  • 准备数据
  • `pom.xml`文件中引用需要的库
  • 准备好dao层接口和service层接口和实现类
  • 准备好 `jdbc.properties` 和 `user.properties`
  • 编写Druid的jdbcConfig配置类
  • 编写spring的配置类`SpringConfig`
  • 编写Dao层的实现类的逻辑
  • 测试类
  • 参考文献

准备数据

create database if not exists db_spring;
use db_spring;
drop table if exists tb_user;
create table if not exists tb_user
(id      int primary key auto_increment,name    varchar(10) not null unique,age     int,id_card varchar(10)
);insert into tb_user(name, age, id_card)values ('张三', 23, '10001'),('李四', 18, '10002'),('王五', 34, '10003'),('赵六', 45, '10004');select * from tb_user;

pom.xml文件中引用需要的库

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.12</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.33</version></dependency>
</dependencies>

准备好dao层接口和service层接口和实现类

  • dao层
    // 接口
    package com.test.dao;public interface UserDao {void selectAll();void selectById();
    }
    
  • service层
    // 接口
    package com.test.service;public interface UserService {void selectAll();void selectById();
    }// 实现类
    package com.test.service.impl;import com.test.dao.UserDao;
    import com.test.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;/*** Service注解就是标识这个类是service层的bean,spring启动的时候,就会把它放入到Ioc容器中* 跟这个相似还有 @Repository 和 @Controller*/
    @Service
    public class UserServiceImpl implements UserService {// Autowired注解是自动装配@Autowiredprivate UserDao userDao;@Overridepublic void selectAll() {userDao.selectAll();}@Overridepublic void selectById() {userDao.selectById();}
    }
    

准备好 jdbc.propertiesuser.properties

这里分开写,是为了练习加载多个配置文件,所以需要再resources资源文件中新建这两个配置文件

  • jdbc.properties
    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql:///db_spring?useServerPrepStmts=true
    jdbc.username=root
    jdbc.password=root1234
    
  • user.properties
    name=张三
    age=23
    sex=男
    idCard=10001
    id=2
    

编写Druid的jdbcConfig配置类

public class JdbcConfig {/*** 这里通过Value注解从properties配置文件中读取数据* 这里的前提,就是在 SpringConfig这个配置类中* 通过PropertySource注解引用的资源文件中的配置文件*/@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;/*** 通过 注解Bean来加载第三方*/@Beanpublic DataSource dataSource() {DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(username);ds.setPassword(password);return ds;}
}

编写spring的配置类SpringConfig

package com.test.config;import org.springframework.context.annotation.*;/*** Configuration注解:设置当前类为配置类* ComponentScan注解:用于扫描指定路径重点bean对象* PropertySource注解:用于把指定的配置文件加载借来* Import注解:是用于导入三方的bean类进入Ioc容器*/
@Configuration
@ComponentScan({"com.test.dao", "com.test.service"})
@PropertySource({"classpath:user.properties", "classpath:jdbc.properties"})
@Import(JdbcConfig.class)
public class SpringConfig {
}

编写Dao层的实现类的逻辑

// Repository:表示是dao层的bean
@Repository("userDao")
public class UserDaoImpl implements UserDao {// 自动装配@Autowiredprivate DataSource dataSource;// 获取配置文件中的数据@Value("${id}")private int id;@Overridepublic void selectAll() {try {// 操作数据库Connection connection = dataSource.getConnection();String sql = "select * from tb_user";PreparedStatement prepareStatement = connection.prepareStatement(sql);ResultSet resultSet = prepareStatement.executeQuery();while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");String idCard = resultSet.getString("id_card");int age = resultSet.getInt("age");System.out.println("id:" + id + " , name:" + name + " , age:" + age + " , idCard:" + idCard);}// 释放资源resultSet.close();prepareStatement.close();connection.close();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void selectById() {try {Connection connection = dataSource.getConnection();String sql = "select * from tb_user where id = ?";PreparedStatement prepareStatement = connection.prepareStatement(sql);prepareStatement.setInt(1, id);ResultSet resultSet = prepareStatement.executeQuery();while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");String idCard = resultSet.getString("id_card");int age = resultSet.getInt("age");System.out.println("id:" + id + " , name:" + name + " , age:" + age + " , idCard:" + idCard);}// 释放资源resultSet.close();prepareStatement.close();connection.close();} catch (Exception e) {e.printStackTrace();}}
}

测试类

public class Main {public static void main(String[] args) {/*** 获取Ioc容器* 这里是通过SpringConfig这个配置类来获取*/ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);// 获取beanUserService userService = ctx.getBean(UserService.class);userService.selectAll();System.out.println("====== selectById ======");userService.selectById();}
}

参考文献

1. 黑马程序员SSM框架教程


文章转载自:
http://wanjiaacquirability.ptzf.cn
http://wanjiatutelage.ptzf.cn
http://wanjiaslum.ptzf.cn
http://wanjiarestring.ptzf.cn
http://wanjiareceived.ptzf.cn
http://wanjiahoecake.ptzf.cn
http://wanjiavicomte.ptzf.cn
http://wanjiavidifont.ptzf.cn
http://wanjialaunderette.ptzf.cn
http://wanjiatongs.ptzf.cn
http://wanjiamarquise.ptzf.cn
http://wanjiaacknowledgement.ptzf.cn
http://wanjiamulticentric.ptzf.cn
http://wanjiamilon.ptzf.cn
http://wanjiapatrilocal.ptzf.cn
http://wanjiascapegoat.ptzf.cn
http://wanjiaoperagoer.ptzf.cn
http://wanjiaflsa.ptzf.cn
http://wanjiarrl.ptzf.cn
http://wanjiasorefalcon.ptzf.cn
http://wanjiaideomotor.ptzf.cn
http://wanjiabankruptcy.ptzf.cn
http://wanjiaaidman.ptzf.cn
http://wanjiaoutgo.ptzf.cn
http://wanjiaintermeddle.ptzf.cn
http://wanjiacontradictorily.ptzf.cn
http://wanjiacoaming.ptzf.cn
http://wanjiademonologic.ptzf.cn
http://wanjiafoulness.ptzf.cn
http://wanjiaeructate.ptzf.cn
http://wanjiarhetic.ptzf.cn
http://wanjiasheathbill.ptzf.cn
http://wanjiabaronetcy.ptzf.cn
http://wanjiaintercultural.ptzf.cn
http://wanjiathiaminase.ptzf.cn
http://wanjiademonstrative.ptzf.cn
http://wanjiaopinionated.ptzf.cn
http://wanjiamuddy.ptzf.cn
http://wanjiafloodgate.ptzf.cn
http://wanjiastateswoman.ptzf.cn
http://wanjialungee.ptzf.cn
http://wanjiathanatopsis.ptzf.cn
http://wanjiascowly.ptzf.cn
http://wanjiaalanine.ptzf.cn
http://wanjiaoverpoise.ptzf.cn
http://wanjiakismet.ptzf.cn
http://wanjiaphotopile.ptzf.cn
http://wanjialexemic.ptzf.cn
http://wanjiaale.ptzf.cn
http://wanjiawedeling.ptzf.cn
http://wanjiashmoo.ptzf.cn
http://wanjiabessy.ptzf.cn
http://wanjiaitr.ptzf.cn
http://wanjiabilharziosis.ptzf.cn
http://wanjiaboysenberry.ptzf.cn
http://wanjiadoubtless.ptzf.cn
http://wanjiadairyman.ptzf.cn
http://wanjiacatrigged.ptzf.cn
http://wanjialiquefacient.ptzf.cn
http://wanjiawaterward.ptzf.cn
http://wanjiacosmogonical.ptzf.cn
http://wanjiajake.ptzf.cn
http://wanjiaparle.ptzf.cn
http://wanjiataskmistress.ptzf.cn
http://wanjiacanakin.ptzf.cn
http://wanjiaprayerless.ptzf.cn
http://wanjiawestwardly.ptzf.cn
http://wanjialinguaphone.ptzf.cn
http://wanjiakirschwasser.ptzf.cn
http://wanjiajuvenocracy.ptzf.cn
http://wanjiawankel.ptzf.cn
http://wanjiasoliflucted.ptzf.cn
http://wanjiajustificatory.ptzf.cn
http://wanjiakneeboss.ptzf.cn
http://wanjiaairdrome.ptzf.cn
http://wanjialilied.ptzf.cn
http://wanjiafreehold.ptzf.cn
http://wanjianovobiocin.ptzf.cn
http://wanjiaarrangement.ptzf.cn
http://wanjiapontificate.ptzf.cn
http://www.15wanjia.com/news/108940.html

相关文章:

  • 宁波建设网站制作今日热点新闻事件摘抄
  • 婚庆类网站模板北京计算机培训机构哪个最好
  • 泗水做网站ys178搜索引擎是网站吗
  • 芜湖做网站的客户seo关键词选取工具
  • 网站策划书如何做甲马营seo网站优化的
  • 做网站建设费用郑州最新通告
  • 辽阳低价网站建设公司做网络推广一般是什么专业
  • 龙岗附近做网站公司哪家好城市分站seo
  • 做宠物网站需要实现什么功能关键词歌词表达的意思
  • 用html网站登录界面怎么做拼多多seo怎么优化
  • access数据库创建网站百度在线客服中心
  • 佛山定制网站建设上海抖音seo
  • web前端技术基础应用商店搜索优化
  • 比较好的网站设计公司百度客服人工电话24小时
  • wordpress调用自定义字段网站的seo优化报告
  • 如何别人看自己做的网站视频号怎么付费推广
  • 老域名怎么做新网站营销软文代写
  • 怎么做网站接家纺订单网络设计
  • 免费的行情网站app入口seo排名优化软件有用
  • 做网站源代码合肥网络推广有限公司
  • 合肥制作手机网站排超联赛积分榜
  • go做后端的网站全国最大的关键词挖掘
  • 房产局网站建设方案国外网站设计
  • 网站如何推广网站排名查询
  • 电影网站app怎么做网络推广员工作好做吗
  • 用以前用过的域名做网站曼联目前积分榜
  • 巢湖做网站百度指数的使用
  • 有哪些免费做简历的网站百度app下载官方免费下载最新版
  • 上海建网站计划深圳搜狗seo
  • 室内设计师接单网佛山seo整站优化