• 快速创建SpringBoot+SSM解析


    此处使用IDEA快速搭建SpringBoot应用,首先用SpringBoot搭建WEB工程:

    image

    image

    image

    然后点击Next生成项目,首次生成可能有点慢,下次创建的时候就会快很多,生成后的目录结构如下:

    image

    我们更改下这个启动类:

    加上@RestController注解使之成为一个Controller,请求能进来,然后加上相应的方法:

    @SpringBootApplication
    
    @RestController
    
    public class SpringbootdemoApplication {
    
    public static void main(String[] args) {
    
        SpringApplication.run(SpringbootdemoApplication.class, args);
    
    }
    
    @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
    
    String index(){
    
        return "Hello Spring Boot!";
    
    }
    

    }

    点击右上角的启动项,启动这个项目:

    image

    然后在浏览器中输入地址:http://localhost:8080/ 可以看到如下效果:

    image

    到此,一个简单的SpringBoot搭建完成了

    2.然后开始我们的第二步,搭建SSM

    在上一步的基础上我们需要添加些相关的配置来搭建SSM,如下为结构图:

    image

    UserController:

    @RestController
    /**自动返回的是json格式数据***/
    public class UserController {
    	
    	@Autowired
    	private UserService userService;
    
    	@RequestMapping("list")
    	public List<User> list(){
    		 //只对紧邻的下一条select语句进行分页查询,对之后的select不起作用
    		List<User> list = userService.findAllUser();
    		return list;
    	}
    }
    
    

    User:

    public class User implements Serializable{
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private Integer id;	
    	private String username;
    	private Date birthday;
    	private String sex;
    	private String address;
    	
    	
    	
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	public Date getBirthday() {
    		return birthday;
    	}
    	public void setBirthday(Date birthday) {
    		this.birthday = birthday;
    	}
    	public String getSex() {
    		return sex;
    	}
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    	public String getUsername() {
    		return username;
    	}
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	
    	
    
    }
    
    

    UserMapper:

    @Mapper
    public interface UserMapper {
    	
    	public List<User> findAll();
    
    }
    

    UserService:

    public interface UserService {
    	
    	/**
    	 * 根据接口查询所用的用户
    	 */
    	public List<User> findAllUser();
    }
    
    

    UserServiceImpl:

    @Service
    public class UserServiceImpl implements UserService {
    		@Resource
    	private UserMapper userMapper;
    		public List<User> findAllUser() {
    		List<User> list = userMapper.findAll();
    		return list;
    	}
    
    }
    

    UserMapper.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" >
    <mapper namespace="com.demo.mapper.UserMapper">
    	<select id="findAll" resultType="com.demo.bean.User">
    		select * from user
    	</select>
    </mapper>
    

    SqlMapperConfig:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <!-- 对在此配置文件下的所有cache进行全局性开/关设置 true|false true -->
            <setting name="cacheEnabled" value="true" />
            <!-- 全局性设置懒加载。如果设为‘关',则所有相关联的都会被初始化加载。 -->
            <setting name="lazyLoadingEnabled" value="true" />
            <!-- 当设置为‘开’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。 -->
            <setting name="aggressiveLazyLoading" value="true" />
            <!-- 允许和不允许单条语句返回多个数据集(取决于驱动需求) -->
            <setting name="multipleResultSetsEnabled" value="true" />
            <!-- 使用列标签代替列名称。不用的驱动器有不同的作法。 -->
            <setting name="localCacheScope" value="STATEMENT" />
            <!-- 允许JDBC生成主键。需要驱动器支持.如果设为了true,这个设置将强制使用被生成的主键, 有一些驱动器不兼容不过仍然可以执行。 -->
            <setting name="useGeneratedKeys" value="true" />
            <!-- 指定MyBatis是否并且如何来自动映射数据表字段与对象的属性。PARTIAL将只自动映射简单的,NONE没有嵌套的结果。 FULL将自动映射所有复杂的结果。 -->
            <setting name="autoMappingBehavior" value="PARTIAL" />
            <!-- 配置和设定执行器,SIMPLE执行器执行其它语句。REUSE执行器可能重复使用preparedstatements语句,BATCH执行器可以重复执行语句和批量更新。 -->
            <setting name="defaultExecutorType" value="SIMPLE" />
            <!-- 设置一个时限,以决定让驱动器等待数据库回应的多长时间为超时. 正整数 -->
            <setting name="defaultStatementTimeout" value="5000" />
            <setting name="jdbcTypeForNull" value="OTHER"/>
            <setting name="logImpl" value="LOG4J"/>
        </settings>
    
    </configuration>
    
    

    application.properties:

    #server.port=80
    
    logging.level.org.springframework=DEBUG
    #springboot   mybatis
    #jiazai mybatis peizhiwenjian
    mybatis.mapper-locations = classpath:mapper/*Mapper.xml
    mybatis.config-location = classpath:mybatis/sqlMapConfig.xml
    #mybatis.type-aliases-package = com.demo.bean
    
    #shujuyuan
    spring.datasource.driver-class-name= com.mysql.jdbc.Driver
    spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    spring.datasource.username = root
    spring.datasource.password = root
    

    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>
    
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>springbootdemo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    
            <!--mybatis与mysql-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.2.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--druid依赖-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.25</version>
            </dependency>
            <!--redis依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
    
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
            <!--jasypt加解密-->
            <dependency>
                <groupId>com.github.ulisesbocchio</groupId>
                <artifactId>jasypt-spring-boot-starter</artifactId>
                <version>1.14</version>
            </dependency>
    
            <!--zipkin-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-sleuth</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-sleuth-zipkin</artifactId>
            </dependency>
    
    
            <!--eureka-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    
    

    项目运行后访问http://localhost:8080/list,可以见到如下结果(这里省略sql表的设计,可以自己根据实体类来建表)
    image.png

  • 相关阅读:
    《民工》随笔
    最近繁忙,暂停更新
    UVA 839 Not so Mobile
    UVA 310 Lsystem
    UVA 10602 Editor Nottoobad
    UVA 10562 Undraw the Trees
    UVA 327 Evaluating Simple C Expressions
    UVA 10954 Add All
    UVA 270 Lining Up
    UVA 699 The Falling Leaves
  • 原文地址:https://www.cnblogs.com/charlypage/p/9291385.html
Copyright © 2020-2023  润新知