首页 > 数据库 > 其他综合 >

Mybatis初级使用-Spring集成mybatis

2017-03-29

Mybatis初级使用-Spring集成mybatis,Spring集成mybatis,也就是SqlSessionFactory、SqlSession不需要硬编码去创建,而是通过Spriing的IOC能力,注入到程序中。

Spring集成mybatis,也就是SqlSessionFactory、SqlSession不需要硬编码去创建,而是通过Spriing的IOC能力,注入到程序中。

最简单案例


maven依赖 (直接添加spring-webmvc,它依赖的其他包会自动载入


  
    log4j
    log4j
    1.2.17
  
  
    mysql
    mysql-connector-java
    5.1.38
  
  
    org.springframework
    spring-webmvc
    4.3.7.RELEASE
  
  
    org.springframework
    spring-jdbc
    4.3.7.RELEASE
  
  
    org.mybatis
    mybatis
    3.4.2
  
  
    org.mybatis
    mybatis-spring
    1.3.0
  
\

resources/spring/spring-mybatis.xml




	

	
	

	
	
		
        
        
        
	

	
		
		
		
		
		
	

	
		
		
	

resources/spring/spring-mvc.xml




	

	

	
			
		jsp">
	


resources/jdbc.properties (用户名不要使用username,否则会与环境中冲突。报 JDBC连接错误,没有权限。)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=luzhen

resources/mybatis-config.xml




    
	
		
	

web.xml



	Archetype Created Web Application

	
		contextConfigLocation
		classpath:spring/spring-mybatis.xml
	

	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	
	
		encodingFilter
		/
	

	
		org.springframework.web.context.ContextLoaderListener
	

	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring/spring-mvc.xml
		
	
	
		springmvc
		/
	

resources/log4j.properties(一定要配置log,这样程序出错,就会更容易找到问题错在)

log4j.rootLogger=DEBUG, stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

com.lz.demo.dao.model.Role.java

package com.lz.demo.dao.model;

public class Role {

	private Integer id;
	
	private String roleName;
	
	private String note;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}
	
}

com.lz.demo.dao.mapper.RoleMapper.java

package com.lz.demo.dao.mapper;

import com.lz.demo.dao.model.Role;

public interface RoleMapper {

	Role getRoleById(Integer id);
	
}

resources/mapper/RoleMapper.xml




	
		
		
		
	
	
		id, role_name, note
	
	

com.lz.demo.controller.HomeController

package com.lz.demo.controller;

import javax.servlet.http.HttpSession;

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 org.springframework.web.bind.annotation.RequestParam;

import com.lz.demo.dao.mapper.RoleMapper;
import com.lz.demo.dao.model.Role;

@Controller
public class HomeController {

	@Autowired
	private RoleMapper roleMapper;

	@RequestMapping("/home")
	public String home(@RequestParam("id") Integer id, Model model) {
		Role role = roleMapper.getRoleById(id);
		if (role == null) {
			role = new Role();
			role.setRoleName("数据库没有ID=" + id + "的Role记录");
			role.setNote("这是Java创建的Role");
		}
		model.addAttribute("role", role);
		
		return "home";
	}

}

/WEB-INF/views/home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page isELIgnored="false" %>
RoleName: ${role.roleName}
Note: ${role.note}

启动Web应用,访问 http://localhost:8080/spring_mybatis/home?id=10001 \ \


相关文章
最新文章
热点推荐