• Idea创建SpringBoot项目整合Hibernate


    然后next

    依赖包选择,Web必须

    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 http://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.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.macao</groupId>
        <artifactId>springboot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot</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-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--springboot依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <!-- JAP -->
            <dependency>
                 <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
            </dependency>
    
            <!-- 数据库驱动以及数据库连接池-->
            <dependency>
                <groupId>org.mariadb.jdbc</groupId>
                <artifactId>mariadb-java-client</artifactId>
                <version>2.3.0</version>
            </dependency>
    
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

    笔者使用的数据库为MariaDB(MySql的一个分支)

    项目目录结构

     application.properties配置:

    spring.datasource.url = jdbc:mariadb://localhost:3306/testdb
    spring.datasource.username = root
    spring.datasource.password = xxx
    spring.datasource.driverClassName = org.mariadb.jdbc.Driver
    spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
    #Spring Data JPA
    spring.jpa.database=MYSQL
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    # stripped before adding them to the entity manager)
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    
    #视图层控制
    spring.mvc.view.prefix=classpath:/templates/
    spring.mvc.view.suffix=.html
    spring.mvc.static-path-pattern=/static/**

    User.java

    @Entity
    @Table(name="user")
    public class User {
    
        @Id
        @GeneratedValue
        private int id;
    
        @Column(name = "name")
        private String name;
    
        @Column(name = "password")
        private String password;
    
        public User() {
        }
    
        public User(int id, String name, String password) {
            this.id = id;
            this.name = name;
            this.password = password;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }

    DAO层UserRepository.java

    /**
     * @author DZ
     * // TODO: 2019/3/11
     * */
    @Repository
    public interface  UserRepositoty extends JpaRepository<User, Long> {
    
        /**
         * Find by name.
         *
         * @param name the name
         * @return the user
         */
        User findByName(String name);
    }

    Service层UserService.java

    /**
     * @author DZ
     * // TODO: 2019/3/11
     * */
    
    public interface UserService {
    
        /**
         * Find by name.
         *
         * @param name the name
         * @return the user
         */
    
        User findByName(String name);
    }

    实现类UserServiceImpl.java

    /**
     * @author DZ
     * // TODO: 2019/3/11
     * */
    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        private UserRepositoty userRepositoty;
    
        @Override
        public User findByName(String name) {
            return userRepositoty.findByName(name);
        }
    }

    Controller层UserController.java

    /**
     * @author DZ
     * // TODO: 2019/3/11
     * */
    @RestController
    public class UserController {
        @Autowired
        private UserService userService;
    
        @RequestMapping("/say")
        String home() {
             return "hello world";
        }
    
       @RequestMapping("/hello/{name}")
        User hello(@PathVariable String name) {
             return userService.findByName(name);
       }
    }

    SpringBootApplication.java

    /**
     * @author DZ 
     * // TODO: 2019/3/11
     * */
    @SpringBootApplication
    @EnableJpaRepositories
    @ComponentScan(basePackages = "com.macao.springboot")
    public class SpringbootApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringbootApplication.class, args);
    
        }
    }

     

  • 相关阅读:
    启动程序相关的命令
    分享的几行代码
    各种大数据软件安装
    tomcat报没法分配内存大小解决方案
    数据库事务
    pytorch之CNN实现
    搜索与匹配
    调试 pytorch 及 python 的 特殊语法
    图神经网络 GCN 等综述(转载)
    关于【finder不能完成该操作 因为未能读取或写入"文件名"中的某些数据(错误代码-36)】(实测,好用)
  • 原文地址:https://www.cnblogs.com/dzcici/p/10511451.html
Copyright © 2020-2023  润新知