美文网首页
spring+jdbcTemplate使用文档

spring+jdbcTemplate使用文档

作者: 星米糯 | 来源:发表于2019-10-08 11:15 被阅读0次

作业


1.建表 英雄表hero(id,name,atk,def,spd)

2.建实体类

3.创建dao类。

4.创建业务类。

5.创建Test。

提示请用户选择要做的操作

1.查看所有的英雄。

2.添加英雄,

3.删除英雄。

4.根据id查看英雄详细信息。

5.退出

1

程序罗列出所有的英雄信息。

提示请用户选择要做的操作

1.查看所有的英雄。

2.添加英雄,

3.删除英雄。

4.根据id查看英雄详细信息。

5.退出

4,

请输入要查询的英雄的编号

1

把id为1的英雄信息展示到控制台上。

提示请用户选择要做的操作

1.查看所有的英雄。

2.添加英雄,

3.删除英雄。

4.根据id查看英雄详细信息。

5.退出

实现项目结构

1:创建数据库和表hero(id,name,atk,def,spd)

使用cmd方式创建:


1.输入password

2.查看已有数据库

show databases;

3.如果使用已有数据库:use xxx;

如果没有,需要创建一个database:create database xxx;

4.查看数据库中的表:show tables;

创建新表:create table xxx(属性);

查看表内容:select * from xxx;

插入数据:insert into xxx values();

在这里插入图片描述

2:创建Hero实体类(entity)

在这里插入图片描述

将表中的列对应到实体类的属性中

3:创建dao类与其实现类(dao)

在这里插入图片描述

4:创建业务类(service)

在这里插入图片描述

5:创建Test(test)

在这里插入图片描述

6:添加项目依赖:

在这里插入图片描述

7:启动spring应用

在这里插入图片描述

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

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

        version="4.0">

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

</web-app>

8.项目的整体目录

在这里插入图片描述

将项目整合起来,使用spring框架ioc控制反转,让spring工厂来new类

使用spring的DI思想,将类依赖注入到其他类的属性中

将项目架构起来

1:如何将一个普通的java项目转换成web项目并用maven管理jar包

在这里插入图片描述

or

在这里插入图片描述

设置成web模式:

在这里插入图片描述

管理项目的文件夹类型

在这里插入图片描述

将项目设置成maven来管理jar包


在这里插入代码片选中项目——>右键——>选择Add Framworks Support——>选择maven

这时项目中会生成pom.xml文件

将pom.xml文件修改成jdk版本为1.8:

    <properties>

        <maven.compiler.source>1.8</maven.compiler.source>

        <maven.compiler.target>1.8</maven.compiler.target>

    </properties>

2:spring框架

spring的核心内容是ioc(控制反转)和aop(面向切面编程)

使用:1.导入相关jar包;


spring相关的jar包:

spring-core:spring的核心jar包

spring-beans:用来管理bean对象的

spring-context:上下文支持jar包

spring-aop:面向切面编程使用的jar包

spring-expression:spring中一个表达式解析jar包

spring-web:支持spring开发jar包,Servlet类

2.在resource目录(src)下创建applicationContext.xml(or spring.xml,命名随意)文件,

在这里插入图片描述

并将其添加到spring的框架中;

在这里插入图片描述

3.在web.xml中启动spring


<!--初始化spring配置-->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!--启动Web容器时,自动装配applicationContext.xml的配置信息,执行它所实现的方法。

如果没有该文件,需在context-param 指定一个Spring容器的初始化配置文件,本例中是applicationContext.xml-->

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

相当于:


public static void main(String[] args) {



    // 得到Spring工厂类,参数是我们配置文件的路径。classpath表示src文件夹,也就是java类编译后的目录

    ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");



    // 根据工厂类创建出对象,参数就是我们配置文件中bean的id属性

    //此时的spring配置文件:

    //<!-- 针对我们写的类进行配置,让spring帮助我们创建类的对象 -->

//<bean id="ua" class="com.baizhi.action.UserAction">

    UserAction ua = (UserAction)ac.getBean("ua");



    //使用spring创建出来的对象...

}

3:使用spring框架(springJDBC)管理对数据库操作,在dao层实现

在spring的配置文件中将数据库属性添加到相关类中:


<!--1.加载db.properties文件 -->

<context:property-placeholder location="classpath:db.properties"/>

db.properties:


jdbc.driverClassName:com.mysql.jdbc.Driver

jdbc.url:jdbc:mysql://localhost:3306/hero?serverTimezone=UTC

jdbc.username:root

jdbc.password:root

注:value中必须以jdbc.xxx的形式来实现值的注入


<!--2.数据源-->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

    <property name="driverClassName" value="${jdbc.driverClassName}"/>

    <property name="url" value="${jdbc.url}"/>

    <property name="username" value="${jdbc.username}"/>

    <property name="password" value="${jdbc.password}"/>

</bean>

注:<bean>中property标签的name必须有对应的set方法(使用set方法依赖注入)


<!--3.spring提供简化JDBC操作的模板JdbcTemplate-->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

    <property name="dataSource" ref="dataSource"/>

</bean>

在这里插入图片描述 在这里插入图片描述

然后可以将jdbcTemplate注入到xxxdaoImpl中使用,


<bean id="dh" class="dao.HeroDaoImpl">

    <property name="jdbcTemplate" ref="jdbcTemplate"/>

</bean>

此时的jdbc中包含有DataSource类,并通过数据源已经和数据库连接上了。

创建实体类对应的dao与daoImpl方法:

实体类:entity


package entity;

/**

* 实体类hero

*/

public class Hero {

    private Integer id;

    //属性...

    //构造方法...

    //get & set...

    //toString...

}

对应的dao接口(将对jdbcTemplate的操作方法写到dao中)


public interface HeroDao {

    void insert(Hero hero)throws Exception;

    void delete(int id)throws Exception;

    void update(Hero hero)throws Exception;

    Hero select(int id)throws Exception;

    List<Hero> selectAll()throws Exception;

}

对应的daoImpl实现类(将jdbcTemplate注入dao中,并实现对jdbcTemplate的相关方法的调用)


package dao;

@Repository

public class HeroDaoImpl implements HeroDao {

    private JdbcTemplate jdbcTemplate;

    //使用的是set注入方法,将jdbcTemplate注入到dao中

    //

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;

    }



    //对dao中的方法的实现

}

4.使用service层将操作包装成业务,调用dao层的方法,添加逻辑处理

xxxService接口


public interface HeroService {

    void save(Hero hero)throws Exception;

    void remove(int id)throws Exception;

    void modify(Hero hero)throws Exception;

    Hero findById(int id)throws Exception;

    List<Hero> findAll()throws Exception;

}

xxxServiceImpl实现类


package service;

@Service

@Transactional(readOnly = true)

public class HeroServiceImpl implements HeroService {

    //使用set注入dao实现类

    //<bean id="ss" class="service.HeroServiceImpl">

    //<property name="heroDao" ref="dh"/>

    //</bean>

    private HeroDao heroDao;

    public void setHeroDao(HeroDao heroDao) {

        this.heroDao = heroDao;

    }

    //添加业务逻辑,调用dao中的方法实现业务

}

5.将servlet用test实现,不再写页面(java or jsp or html+templates)了


import ...

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class HeroServlet {

    //将业务类注入

    @Autowired

    private HeroService heroService;

    @Test

    public void menu() throws Exception {

        try {

            do {

                //调用业务

                }

            } while (choose != 6);

        }catch (InputMismatchException e){

            System.out.println("输入格式有误");

            menu();//回调

        }

    }

}

所遇到的问题

1:jdbcTemplate为null

1.已经注入到spring框架中的类不用再new了

2:将spring和junit结合起来

1>test类输入台无法进行输入操作

Help-->Edit Custom VM Options...


-Deditable.java.test.console=true

2>test中无法引用junit

不能把测试类命名为Test

3:applicationContext.xml没有加载到spring中

在这里插入图片描述

4:数据库用户名乱码

在配置文件中加上

在这里插入图片描述

相关文章

网友评论

      本文标题:spring+jdbcTemplate使用文档

      本文链接:https://www.haomeiwen.com/subject/mdqbpctx.html