eclipse搭建简单springboot-mybatis项目实现页面简单跳转

1.项目简介:

用eclipse工具搭建一个简单的springboot-mybatis-maven项目实现页面简单跳转。

环境:eclipse,jdk1.8,maven


2.项目目录:

eclipse搭建简单springboot-mybatis项目实现页面简单跳转_第1张图片

3.前期配置

3.1pom.xml



	4.0.0

	com.example
	spring-boot-mybatis1
	0.0.1-SNAPSHOT
	jar

	spring-boot-mybatis1
	Demo project for Spring Boot


	
		UTF-8
		UTF-8
		1.8
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.6.RELEASE
		 
	

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

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

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

	

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



3.2application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.3log4j.properties

#config root logger
log4j.rootLogger = INFO,system.out
log4j.appender.system.out=org.apache.log4j.ConsoleAppender
log4j.appender.system.out.layout=org.apache.log4j.PatternLayout
log4j.appender.system.out.layout.ConversionPattern=[Log] %5p[%F:%L]:%m%n

#config this Project.file logger
log4j.logger.thisProject.file=INFO,thisProject.file.out
log4j.appender.thisProject.file.out=org.apache.log4j.DailyRollingFileAppender
log4j.appender.thisProject.file.out.File=logContentFile.log
log4j.appender.thisProject.file.out.layout=org.apache.log4j.PatternLayout

3.4数据库表设计

eclipse搭建简单springboot-mybatis项目实现页面简单跳转_第2张图片



4.实体类

package com.example.proj;

public class User
{
    private int id;
    private String name;
    private String pwd;
    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 getPwd()
    {
        return pwd;
    }
    public void setPwd(String pwd)
    {
        this.pwd = pwd;
    }
    
}

5.UserMapper

package com.example.mapper;

import java.util.List;

import com.example.proj.User;

public interface UserMapper
{
    public User findByName(String name);
}

7.UserMapper,xml




   
        
        
        
       
    
           

8.service

package com.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.mapper.UserMapper;
import com.example.proj.User;

@Service
public class UserService
{
    @Autowired
    private UserMapper userMapper;
    
    public User findByName(String name){
        return userMapper.findByName(name);
    }
}

9.UserController

package com.example.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.proj.User;
import com.example.service.UserService;


@Controller
public class UserController
{
    
    private Logger logger =Logger.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    
    @RequestMapping("/getUserInfo/{name}")
    @ResponseBody
    public ModelAndView getUserInfo(@PathVariable String name) {
        User user = userService.findByName(name);
        if(user!=null){
            System.out.println("user.getName():"+user.getName());
            logger.info("user.getPwd():"+user.getPwd());
        }
        return new ModelAndView("login");
    }
    
    @RequestMapping("/getUserInfo1")
    @ResponseBody
    public ModelAndView getUserInfo1(@RequestParam(value="name") String name, Model model) {
        User user = userService.findByName(name);
        model.addAttribute("name", user);
        if(user==null){
            return new ModelAndView("error");
        }
        System.out.println("user.getName():"+user.getName());
        logger.info("user.getPwd():"+user.getPwd());
        return new ModelAndView("login");
    }
    
}

10.Application

package com.example;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
@MapperScan("com.example.mapper")
public class Application {
    private static Logger logger = Logger.getLogger(Application.class);

    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }


    /**
     * Start
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        logger.info("启动完毕。。。");
    }

}

11.默认页面index.html

eclipse搭建简单springboot-mybatis项目实现页面简单跳转_第3张图片





index


	

登录首頁


12.登录成功后跳转页面login.html



    demo
    


登录成功


13.启动application

eclipse搭建简单springboot-mybatis项目实现页面简单跳转_第4张图片


14.访问

eclipse搭建简单springboot-mybatis项目实现页面简单跳转_第5张图片


15.登录成功跳转页面

eclipse搭建简单springboot-mybatis项目实现页面简单跳转_第6张图片

16.一个简单的页面跳转项目就写好了。附上项目代码链接:http://pan.baidu.com/s/1eSrLA74 密码:qtwd


你可能感兴趣的:(spring)