美文网首页
尚学堂101天总结+SpringBoot

尚学堂101天总结+SpringBoot

作者: 人间草木为伴 | 来源:发表于2019-11-25 14:08 被阅读0次

    099天
    行百里者半九十,想要在一个行业里成为顶尖人才,一定满足一万小时定律,要想学好JAVA,需要持之以恒不断地努力,每天都要勤思考+善于询问+解决问题!

    知识温故而知新>>>>>>

    IDEA设置代码快捷联想键的方法

    1111.png

    控制台运行java的jar包的方法

    控制台运行JAVA项目的方法(先将项目打包)

    单环境时, 只有一个配置文件
    运行命令:java -jar 项目的名称
    
    单环境时, 有多个配置文件
    语法结构:application-{profile}.properties/yml
    profile:代表某个配置环境的标识
    示例:application-dev.properties/yml 开发环境
    application-test.properties/yml 测试环境
    application-prod.properties/yml 生产环境
    启动命令:java -jar xxx.jar --spring.profiles.active={profile}
    
    Maven项目的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.sxt</groupId>
        <artifactId>springdata-jpa</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springdata-jpa</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
                <version>5.1.38</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    application.properties
    #配置数据源信息
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/jpa
    spring.datasource.username=root
    spring.datasource.password=root
    #配置数据库连接池的类型
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    #开启正向工程(根据实体类和映射信息生成数据表) create:创建(会造成已存在的数据丢失)  update:创建或更新(推荐)
    spring.jpa.hibernate.ddl-auto=update
    #是否打印生成sql语句
    spring.jpa.show-sql=true
    

    package com.sxt.springdatajpa.dao.UsersRepository

    import com.sxt.springdatajpa.pojo.Users;
    import org.springframework.data.jpa.repository.JpaRepository;
    public interface UsersRepository  extends JpaRepository<Users,Integer> {
    }
    

    com.sxt.springdatajpa.dao.UsersRepositoryByName

    package com.sxt.springdatajpa.dao;
    
    import com.sxt.springdatajpa.pojo.Users;
    import org.springframework.data.repository.Repository;
    
    import java.util.List;
    
    /**
     *Repository接口的方法名称命名查询
     */
    public interface UsersRepositoryByName extends Repository<Users,Integer> {
        //方法的名称必须要遵循驼峰式命名规则。findBy(关键字)+属性名称(首字母要大写)+查询条件(首字母大写)
        List<Users> findByName(String name); // where name=?
    
        List<Users> findByNameAndAge(String name,Integer age); //where name=? and age=?
    
        List<Users> findByNameLike(String name); // where name like ?
    
    }
    

    SpringdataJpaApplicationTests

    @SpringBootTest
    class SpringdataJpaApplicationTests {
        @Autowired
        private UsersRepository usersRepository;
        //测试添加用户
        @Test
        public void testAdd(){
            Users users = new Users();
            users.setName("张三");
            users.setAge(20);
            users.setAddress("北京");
            usersRepository.save(users);
        }
        //测试查询所有
        @Test
        public void testFindAll(){
           List<Users> list = usersRepository.findAll();
            for(Users user:list){
                System.out.println(user);
            }
        }
        //测试根据主键进行查找
        @Test
        public void testFindById(){
           Optional<Users> optional =  usersRepository.findById(1);
           Users user = optional.get();
           System.out.println(user);
        }
    
        //测试修改
        @Test
        public void testUppdate(){
            Users users = new Users();
            users.setId(1);
            users.setName("王五");
            users.setAge(30);
            users.setAddress("上海");
            usersRepository.save(users);//新增和修改都使用save方法,类似与hibernate中的saveOrUpdate方法
        }
        //测试删除
        @Test
        public void testDelete(){
            usersRepository.deleteById(2);
        }
        
        @Test
        public void testUserRepository(){
            System.out.println(usersRepository);
            System.out.println(usersRepository.getClass());
        }
        }
    

    TestUsersRepositoryByName

    package com.sxt.springdatajpa;
    
    import com.sxt.springdatajpa.dao.UsersRepositoryByName;
    import com.sxt.springdatajpa.pojo.Users;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    /**
     *
     //测试Repository接口的命名规则查询
     */
    @SpringBootTest
    public class TestUsersRepositoryByName {
        @Autowired
        UsersRepositoryByName usersRepositoryByName;
        //测试按照名称进行精确查找
        @Test
        public void testFindByName(){
          List<Users> list = usersRepositoryByName.findByName("王五");
          for(Users user:list){
              System.out.println(user);
          }
        }
    
        //测试按照名称和年龄进行精确查找
        @Test
        public void testFindByNameAndAge(){
            List<Users> list = usersRepositoryByName.findByNameAndAge("王五",30);
            for(Users user:list){
                System.out.println(user);
            }
        }
    
        //测试按照名称进行模糊查找
        @Test
        public void testFindByNameLike(){
            List<Users> list = usersRepositoryByName.findByNameLike("%王%");
            for(Users user:list){
                System.out.println(user);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:尚学堂101天总结+SpringBoot

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