创建Spring项目并连接数据库

创建Spring项目并连接数据库

1 先开启Mysql

首先按下电脑的win键,输入cmd,在显示出来的命令提示符里,鼠标右键选择以管理员方式打开。

创建Spring项目并连接数据库_第1张图片

然后再输入命令,net start MySQL。

创建Spring项目并连接数据库_第2张图片

2 打开图形化界面工具Navicat

先创建一个数据库,数据库的字符集选择utf-8,然后再在数据库中建表,在创建主键的时候,选择自动递增 ,最后再保存数据库。

创建Spring项目并连接数据库_第3张图片

3 打开IDEA,创建Spring的项目

创建Spring项目并连接数据库_第4张图片

4 导入相关依赖包

 <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>5.3.8version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>5.3.8version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.3.8version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>5.3.8version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>5.3.8version>
    dependency>

    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>1.2version>
    dependency>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.20version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>5.3.8version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>5.3.8version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>5.3.8version>
    dependency>

5 创建Spring Config

创建Spring项目并连接数据库_第5张图片

6 进行Spring的配置,在applicationContext中

其中的数据库名称和密码要根据你自己创建的做相应的改变


    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        
        <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/>
        
        <property name="username" value="root">property>
        
        <property name="password" value="123456">property>
    bean>
    
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        
        <property name="dataSource" ref="dataSource">property>
    bean>

7 创建类和Dao层

创建Spring项目并连接数据库_第6张图片

Dao层
package com.springjdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//用注解创建的对象就是类本身的类型,名字就是这个类的名字,首字母小写personDao
@Repository
public class PersonDao {
    //    1 实现依赖注入
//    2 生成成员变量get和set方法
//    3 实现自动注入 byType->byName (@Resource功能类似,只是该注解优先采用根据名称自动装配)
    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<Person> findAllUser(){
        RowMapper<Person> rowMapper = new BeanPropertyRowMapper<>(Person.class);
        return jdbcTemplate.query("select * from person",rowMapper);
    }

    public void addUser(String name,int age){
        jdbcTemplate.update("insert into person values (null,?,?)",age,name);
    }//?为占位符

    public void updateUser(String name,int age,int id){
        jdbcTemplate.update("update person set name = ?,age = ? where id = ?",name,age,id);
    }

    public void deleteUser(int id){
        jdbcTemplate.update("delete from person where id = ?",id);
    }
}
类层
package com.springjdbc;

public class Person {
    private int id;

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    private int age;
    private String name;

}
App层
package com.springjdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//加载spring容器,创建对象
        PersonDao personDao = (PersonDao) applicationContext.getBean("personDao");
        personDao.addUser("小花",18);
//        personDao.updateUser("小光",20,1);
//        personDao.deleteUser(1);
        List<Person> personList = personDao.findAllUser();
        for(Person person : personList){
            System.out.println(person.getName()+"  "+person.getAge());
        }
        System.out.println( "Hello World!" );
    }
}
applicationContext

<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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="com.springjdbc"/>
    
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        
        <property name="url" value="jdbc:mysql://localhost:3308/spring?characterEncoding=utf8&serverTimezone=UTC"/>
        
        <property name="username" value="root">property>
        
        <property name="password" value="123456">property>
    bean>
    
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        
        <property name="dataSource" ref="dataSource">property>
    bean>
beans>

你可能感兴趣的:(Spring,spring,big,data,数据库)