SpringBoot Mybatis项目搭建

思路:使用STS开发工具,引入相关依赖包,在application.properties中配置相关信息,编写实体层、服务层、控制层相关代码

一.STS新建SpringBoot项目

SpringBoot Mybatis项目搭建_第1张图片

SpringBoot Mybatis项目搭建_第2张图片

二.在pom.xml中引入相关依赖


	4.0.0
	com.boot
	testSpringBoot
	0.0.1-SNAPSHOT
	testSpringBoot
	jar
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.9.RELEASE
		
	
	
		
			org.springframework.boot
			spring-boot-starter-web
            1.3.5.RELEASE
		
		
		
		
		    org.mybatis.spring.boot
		    mybatis-spring-boot-starter
		    1.3.1
		
		
		
		
		    org.springframework.boot
		    spring-boot-starter-thymeleaf
		
		  
		      net.sourceforge.nekohtml
		      nekohtml
		      1.9.22
		 
		
	    
	        mysql
	        mysql-connector-java
	    
	     
	      org.springframework.boot
	      spring-boot-starter-test
	    
	    
		
		   org.springframework.boot
		   spring-boot-starter-aop
		
	
	
	
		
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	

项目结构如下:

SpringBoot Mybatis项目搭建_第3张图片

三.配置application.properties相关信息

#配置模板信息
spring.thymeleaf.mode= LEGACYHTML5
spring.thymeleaf.encoding= UTF-8
spring.thymeleaf.content-type= text/html
# 开发禁用缓存
spring.thymeleaf.cache= false
#配置数据源
spring.datasource.url= jdbc:mysql://localhost:3306/cncert?characterEncoding=UTF-8&useSSL=true
spring.datasource.username= root
spring.datasource.password= #####
# 可省略驱动配置, sprin-boot 会由url检测出驱动类型
# driver-class-name: com.mysql.jdbc.Driver
spring.datasource.hikari.connection-timeout= 60000 
mybatis.mapperLocations= classpath:com/xuan/mapper/*.xml
mybatis.typeAliasesPackage= com.xuan.cncert.entity
# spring-boot默认打印输出info级别以上的,可在此处修改输出级别
logging.level.root= info

 

四.编写实体层代码

public class User {  
    private Integer userid;  
    private String username;  
    private String password;  
    private String email; 
    private String account;
    
    public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}


      
}  

五.编写Dao层

@Mapper
public interface UserDao {  
	/**
	 * 根据用户相关信息查找用户
	 * @param user
	 * @return
	 */
    public User getUserByUserInfo(User user);
    /**
     * 增加用户
     * @param user
     * @return
     */
    public Integer addUser(User user);
  
}  

六.编写service层

public interface UserService {
	/**
	 * 根据用户相关信息查找用户
	 * @param user
	 * @return
	 */
	User getUserByUserInfo(User user); 
	/**
	 * 增加用户
	 * @param user
	 * @return
	 */
	Integer addUser(User user);
}
@Service
public class UserServiceImpl implements UserService{
	@Autowired  
	private UserDao userDao;  
	/**
	 * 根据用户相关信息查找用户
	 * @param user
	 * @return
	 */
	@Override
	public User getUserByUserInfo(User user) {
		return userDao.getUserByUserInfo(user);  
	}
	/**
	 * 增加用户
	 */
	@Override
	public Integer addUser(User user) {
		return userDao.addUser(user);  
	}

}

七.编写mapper.xml文件

    
  
  
  
      
          
          
          
          
      
      
     
      
      
    
        INSERT INTO userinfo(account,
        password, email)
        VALUES(#{account}, #{password}, #{email})
    
 

八.编写Controller

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired 
	private UserService userService; 
	
	@GetMapping(value = "/hello")
    public String hello() {
    	return "hello";
    }
	/**
	 * 校验登录信息
	 * @param model
	 * @param request
	 * @param session
	 * @return
	 */
	@PostMapping(value = "loginCheck")
	@ResponseBody 
	public void loginCheck(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){      
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String result = "";
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  //设置获取请求的参数方式为utf-8 能够解析中文参数
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String account = request.getParameter("account");
        String password = request.getParameter("password");
        User checkUser = new User();
        checkUser.setAccount(account);
        checkUser.setPassword(password);
        User getUser = userService.getUserByUserInfo(checkUser);
        if(getUser != null){
        	session.setAttribute("user", getUser);
        	result = "success";
        }else{
        	result = "error";
        }
        out.print(result);
		out.flush();
		out.close();   
    } 
	/**
	 * 校验注册信息
	 * @param model
	 * @param request
	 * @param session
	 * @return
	 */
	@PostMapping(value = "registerCheck")
	@ResponseBody 
	public void registerCheck(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){      
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String result = "";
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  //设置获取请求的参数方式为utf-8 能够解析中文参数
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String account = request.getParameter("account");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        User checkUser = new User();
        checkUser.setAccount(account);
        User getUser = userService.getUserByUserInfo(checkUser);
        if(getUser != null){
        	result = "error";
        }else{
        	User addUser = new User();
        	addUser.setAccount(account);
        	addUser.setPassword(password);
        	addUser.setEmail(email);
        	Integer num = userService.addUser(addUser);
            session.setAttribute("user", addUser);
        	result = "success";
        }
        out.print(result);
		out.flush();
		out.close();   
    } 
	
}

九.测试

SpringBoot Mybatis项目搭建_第4张图片

你可能感兴趣的:(Spring,boot,Mybatis)