springboot与redis整合案例(下)

pom.xml中


	4.0.0

	com.bwie
	zhoukao03
	0.0.1-SNAPSHOT
	jar

	zhoukao03
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.6.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			org.springframework.boot
			spring-boot-starter-redis
			1.3.1.RELEASE
		

		
			org.springframework.boot
			spring-boot-starter-jdbc
		

		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.1.1
		

		
		
			mysql
			mysql-connector-java
		


		
		
			org.springframework.boot
			spring-boot-starter-freemarker
		




		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		



		
		
			org.projectlombok
			lombok
			1.16.8
			provided
		


	


	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


application.properties中的配置

 
  
spring.datasource.url =jdbc:mysql://localhost:3306/***_test

spring.datasource.username = root

spring.datasource.password = root

spring.datasource.driverClassName =com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#启动request对象在模版中的使用
spring.freemarker.request-context-attribute=request
#设定静态资源
spring.mvc.static-path-pattern=/**
#默认的页面后缀为.ftl  可以修改为.html
server.port=8080
server.context-path=/xiangmu
mybatis.type-aliases-package=com.bwie.pojo


#redis配置
spring.redis.host=192.168.202.132
spring.redis.port=6379

list.ftl中 



    
    
<#assign ctx = request.contextPath />

    
    




<#if name?exists>
${name}




<#list goods as g>
    
请选择 商品名称 商品单价 商品数量
${g.gname} ${g.price}
Mapper接口中
 
  
package com.bwie.dao;

import com.bwie.pojo.Goods;
import com.bwie.pojo.Order;
import com.bwie.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by Administrator on 2017/8/21.
 */
@Mapper
public interface UserMapper {


    @Insert("insert into t_user (username,password)values(#{username},#{password})")
    public void insert(User user);

    @Select("select * from t_user where username=#{username} and password=#{password}")
public User login(User user);

    @Select("select * from t_goods ")
    public List selectGoods();

    @Select("select * from t_user where username=#{username}")
    public User selectByName(String username);

    @Select("insert into  t_order (user_id,goods_id,goods_acount)values(#{user_id},#{goods_id},#{goods_acount})")
    public void save(Order order);


}
controller层
 
  
package com.bwie.controller;

import com.bwie.pojo.Goods;
import com.bwie.pojo.Order;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.util.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/8/21.
 */

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("insert")
    public String insert(User user, HttpServletRequest request){

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //将username和password封装成一个user对象,保存到数据库中
           user.setUsername(username);
        user.setPassword(password);
             userService.insert(user);
        //将username作为键,将password作为值,存入到redis数据库中
        Jedis jedis = JedisUtils.getJedis();
        jedis.set(username,password);

        return "login";
    }

    @RequestMapping("toinsert")
    public String toinsert(){
        return "insert";
    }

    @RequestMapping("login")
    public String login(User user, HttpServletRequest request, HttpSession session){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //获取jedis对象
        Jedis jedis = JedisUtils.getJedis();
        //根据username从redis数据库中查询对应的值
        String pwd = jedis.get(username);
        User user1 = userService.selectByName(username);
        System.out.println("---------------------"+user1.getId());
        if(pwd != null && pwd.equals(password)){
            session.setAttribute("uid",user1.getId());
            session.setAttribute("name",username);
            return "redirect:select";
        }else{
            session.setAttribute("mess","y用户名或密码错误");
            return "login";
        }

    }

    @RequestMapping("select")
    public String select(Model model){
        List goods = userService.selectGoods();
        model.addAttribute("goods",goods);
        return "list";
    }

    @RequestMapping("save")
    public String save(Model model,int uid,String [] id,String acounts []){
        System.out.println(acounts.toString());
        for (int i=0;i

utils包连接redis的方法
 
  
package com.bwie.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtils {
    /*
    redis的第一种连接方法
     */
    private final static JedisPool POOL;
    static {
        JedisPoolConfig config = new JedisPoolConfig();

        config.setMaxTotal(50);
        config.setMaxIdle(10);

        POOL = new JedisPool(config, "192.168.202.132", 6379);
    }
    public static Jedis getJedis() {
        return POOL.getResource();
    }
}
 
  
package com.bwie.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

/**
 * Created by Administrator on 2017/8/22.
 */
@Component
public class JedisUtils_2 {

    /*
    第二种连接redis的方法
     */
    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private Integer redisPort;

    private JedisPool POOL;

    @PostConstruct
    public void initJedisPool() {

        JedisPoolConfig config = new JedisPoolConfig();

        config.setMaxTotal(50);
        config.setMaxIdle(10);

        POOL = new JedisPool(config, redisHost, redisPort);
    }


    @Bean
    public Jedis getJedis() {
        return POOL.getResource();
    }

}


 
  
 
  
 
  
 
  

你可能感兴趣的:(springboot与redis整合案例(下))