Spring+SpringMVC+Mybatis整合Demo(Maven项目)

创建项目

1. 先上项目demo目录:

Spring+SpringMVC+Mybatis整合Demo(Maven项目)_第1张图片

2. pom.xml的完整内容为


	4.0.0
	com.san
	ssm-demo
	war
	0.0.1-SNAPSHOT
	ssm-demo Maven Webapp
	http://maven.apache.org

	
		4.1.2.RELEASE
	

	
		
			junit
			junit
			3.8.1
			test
		
		
			javax.servlet
			javax.servlet-api
			3.1.0
		
		
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-aop
			${spring.version}
		
		
			org.springframework
			spring-aspects
			${spring.version}
		
		
			org.springframework
			spring-tx
			${spring.version}
		
		
			org.springframework
			spring-jdbc
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		
		
		
		
			org.mybatis
			mybatis
			3.3.0
		
		
		
			org.mybatis
			mybatis-spring
			1.2.3
		
		
		
			mysql
			mysql-connector-java
			5.1.38
		
		
		
		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		
		
		
		
			org.apache.logging.log4j
			log4j-core
			2.8.2
		
		
		
		
			jstl
			jstl
			1.2
		

	
	
		ssm-demo
	

3. web.xml中的完整内容为



	 
	
	
		contextConfigLocation
		classpath:spring-application.xml
	

	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
	

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

4. spring-mvc.xml的完整代码为




	
	
	
	
	
	

	
	
		
		
	

5. spring-application.xml的完整代码为




	
	
		
			
				classpath:jdbc.properties
				
			
		
	

	
	
		
		
			${jdbc_driverClassName}
		
		
			${jdbc_url}
		
		
			${jdbc_username}
		
		
			${jdbc_password}
		
	

	
	
		
		
		
	
	
	
	
		
	

	
	

6. jdbc.properties的完整代码为

jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/mydemo?useSSL=false
jdbc_username=root
jdbc_password=123456

7. mybatis-config.xml的完整代码为,暂没加配置项


  
 
   

代码展示

1. model块的User.java代码为

 public class User {
	private Integer userId;  
    private String userName;  
    private String userPassword;  
 
    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 getUserPassword() {  
        return userPassword;  
    }  
  
    public void setUserPassword(String userPassword) {  
        this.userPassword = userPassword;  
    } 
}

2. Dao层的UserDao.java代码为

 public interface UserDao {
	public User selectUserById(Integer userId);

}

3. Service层的接口UserService.java代码为

public interface UserService {
	User selectUserById(Integer userId);  
}

4. Service层的实现UserServiceImpl.java代码为

@Service
public class UserServiceImpl implements UserService {
	@Autowired  
    private UserDao userDao;  
  
    public User selectUserById(Integer userId) {  
        return userDao.selectUserById(userId);  
          
    } 
}

5. Controller层的UserController.java代码为

@Controller
@RequestMapping("/user")
public class UserController {
	
    @Resource  
    private UserService userService;  
      
    @RequestMapping("/userhello")    
    public ModelAndView getIndex(){   
        ModelAndView mav = new ModelAndView("index");   
        User user = userService.selectUserById(1);  
        System.out.println(user.getUserName() + user.getUserPassword());
        mav.addObject("hello", "Hello Spring MVC!!!");
        mav.addObject("user", user);
        return mav;    
    }
}

6. 映射文件:UserMapper.xml代码为

  


	
	
		
		
		
	
	
	

7. index.jsp中的代码为:

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


	

${hello}


用户名: ${user.userName}
密码: ${user.userPassword}

数据库部分

1 .创建mydemo的数据库

2. 创建表

CREATE TABLE `t_user` (
`USER_ID` int(11) NOT NULL AUTO_INCREMENT,
`USER_NAME` char(30) NOT NULL,
`USER_PASSWORD` char(10) NOT NULL,
PRIMARY KEY (`USER_ID`),
KEY `IDX_NAME` (`USER_NAME`)
)ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

3. 插入数据

INSERT INTO t_user (USER_ID, USER_NAME,USER_PASSWORD) VALUES (1, 'Zhang San', '654321');
INSERT INTO t_user (USER_ID, USER_NAME,USER_PASSWORD) VALUES (2, 'Li Si', '123456');

运行测试

启动tomcat,运行项目,在浏览器中输入

http://localhost:8080/user/userhello

显示结果为

Spring+SpringMVC+Mybatis整合Demo(Maven项目)_第2张图片

注:有些同学可能需要输出项目名加映射路径才能访问,或者项目会报404,可参考:

https://blog.csdn.net/x_san3/article/details/81560608  看看是不是这个问题。

源码下载地址

CSDN:https://download.csdn.net/download/x_san3/10593561

你可能感兴趣的:(Java框架)