• 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);
    
        }
    }

     

  • 相关阅读:
    software 的魅力
    CSS中,脚本不能覆盖CSS的!!!
    取消自增ID.
    [求教]FF与IE 的Style 不兼容问题?
    SQL存储过程 之 sp_MSforeachtable和sp_MSforeachDB
    疑问:AddWebPart 不能添加动态加载的自定义控件吗?
    关于 各语言的 readonly。
    查询存储过程中的结果集.(顺便贴:一行折多行的方法)
    附一张css hack
    远程桌面dos开启
  • 原文地址:https://www.cnblogs.com/dzcici/p/10511451.html
Copyright © 2020-2023  润新知