SpringBoot——项目搭建、整合Mybatis、整合redis(集群)

一、项目搭建

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>cn.x5456</groupId>
    <artifactId>springBootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!--从这开始拷贝-->

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!--mybatis整合SpringBoot的jar包。从这下面的暂时不拷-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- 加入redis起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>

    </dependencies>

    <!--到这结束-->
    
</project>

程序的主方法mySpringBootDemo.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 扫描所有的类
 * 并将当前类对象交给Spring管理
 */
@SpringBootApplication
public class mySpringBootDemo {

    public static void main(String[] args) {

        // 项目入口
        SpringApplication.run(mySpringBootDemo.class,args);

    }

}

Controller(返回的都是json格式数据)

package cn.x5456.controller;


import cn.x5456.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * rest风格的controller(返回json数据)
 * 相当于@Controller+@ResponseBody注解
 */
@RestController
public class helloController {

    /**
     * 返回json格式数据
     * @return
     */
    @RequestMapping("hello")
    public String hello(){
        return "hello,world!";
    }

    /**
     * 返回pojo
     */
    @RequestMapping("pojo")
    public User pojo(){

        User u = new User();

        u.setName("张三");
        u.setAge(19);

        return u;
    }

    /**
     * 返回集合
     */
    @RequestMapping("list")
    public List<User> list(){

        ArrayList<User> uList = new ArrayList<>();

        User u1 = new User();
        u1.setName("张三");
        u1.setAge(19);


        User u2 = new User();
        u2.setName("李四");
        u2.setAge(20);

        uList.add(u1);
        uList.add(u2);

        return uList;
    }

    /**
     * 返回map
     */
    @RequestMapping("map")
    public Map<String,Object> map(){

        HashMap<String, Object> map = new HashMap<>();

        map.put("a",1);
        map.put("b",2);

        return map;

    }

}

二、SpringBoot与Mybatis整合

1)导入相关jar包

<!--mybatis整合SpringBoot的jar包-->
<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.6</version>
</dependency>

2)创建文件夹,并书写配置文件

sqlMapConfig.xml

<?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>		
	<!--虽然是空的,但必须有-->
</configuration>

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="cn.x5456.mapper.UserMapper">	<!-- 此处的命名空间必须能点到响应的mapper -->
	<select id="findAll" resultType="User">
		select * from user
	</select>
</mapper>

application.properties

#spring集成Mybatis环境

# 1.设置别名
mybatis.type-aliases-package=cn.x5456.domain
# 2.加载Mybatis配置文件
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
# 3.配置数据库连接
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mybatis01?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 5456

3) 代码实现

UserMapper.java(接口,与上面usermapper.xml配置文件对应)

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import cn.x5456.domain.User;
@Mapper
public interface UserMapper {
	
	public List<User> findAll();

}

UserServiceImpl.java

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

UserController.java

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("allUser")
    public List<User> allUser(){

        List<User> allUser = userService.findAllUser();

        return allUser;
    }
}

三、SpringBoot整合redis单机版

1)在application.properties中添加如下代码

#Spring与redis整合

spring.redis.host=127.0.0.1
spring.redis.port=6379

2)在主程序上添加一个注解

/**
 * 扫描所有的类
 * 并将当前类对象交给Spring管理
 */
@EnableCaching  // 开启缓存
@SpringBootApplication
public class mySpringBootDemo {

    public static void main(String[] args) {

        // 项目入口
        SpringApplication.run(mySpringBootDemo.class,args);

    }

}

3)在Service层中需要缓存的方法上添加Cacheable注解

/**
 * 测试缓存,必须要有一个value,作为redis中缓存的键
 * @return
 */
@Cacheable(value = "findAll")
public List<User> findAllUser() {
	List<User> list = userMapper.findAll();
	return list;
}

四、SpringBoot整合redis集群

 

转载于:https://www.cnblogs.com/x54256/p/9156676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值