spring_boot_入门02

Posted 小猪会拱菜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring_boot_入门02相关的知识,希望对你有一定的参考价值。

spring boot   整合
一,创建一个

二,创建数据库

 

 

 

三,导入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

四,向  application.properties  文件插入

插入的代码:

#配置数据库的四个连接参数
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8080/appinfodb
spring.datasource.username=root
spring.datasource.password=123


#Spring Data JPA的配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent-output=true
spring.jpa.database=mysql

五,开写代码

1,创建Userdao

@Repository("iUsersDao")
public interface IUsersDao {
    //登录
    Users getlogin(@Param("userName") String userName, @Param("password") String password);

    //查看
    List<Users> getAll();
}

2.编写Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.example.dao.IUsersDao">
    <!--登录-->
    <select id="getlogin" resultType="com.example.entity.Users">
      SELECT * FROM users WHERE userName=#{userName} AND PASSWORD=#{password}
    </select>

    <!--查询用户-->
    <select id="getAll" resultType="Users">
        select * from users
    </select>
</mapper>

3.编写Userservice

public interface IUsersService {
    //登录
    Users getlogin(String userName, String password);

    //查看
    List<Users> getAll();
}

4.编写Userseviceimpl

@Service("iUsersService")
public class IUsersServiceImpl implements IUsersService {
    @Resource(name = "iUsersDao")
    private IUsersDao iUsersDao;


    @Override
    public Users getlogin(String userName, String password) {
        return iUsersDao.getlogin(userName,password);
    }

    @Override
    @Transactional
    public List<Users> getAll() {
        return iUsersDao.getAll();
    }
}

5.创建启动类

@SpringBootApplication
@MapperScan("com.example.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

以上是关于spring_boot_入门02的主要内容,如果未能解决你的问题,请参考以下文章

用_DockerGradle_来构建运行发布一个_Spring_Boot_应用

Spring_Boot_2精髓_从构建小系统到架构分布式大系统pdf

spring_boot打jar包及打包错误的解决方法

spring_boot 学习随笔

Cg入门19:Fragment shader - 片段级模型动态变色

spring_boot启动报错