全注解实现SSM

阅读更多

本文搭建的SSM框架,即Spring,Struts2,MyBatis。本次搭建采用了Maven对项目进行管理

 

1:建立maven的web项目

 

2:数据库中新建表,本文中,采用的是mysql数据库

 

3:进行配置文件的配置

全注解实现SSM_第1张图片

此时,可关注以上配置文件,其中beans.xml是Spring的xml文件,mybatis.xml是Mybatis的配置,struts.xml是struts2的xml文件。generatorConfig是生成mapping,dao和domain的工具,稍后将做介绍。

 

beans.xml

 



	
	
	
	

	
	
		
		
			
		
		
		
		
		
	

	
	
		
		
			
				classpath:cn/wind/mapping/*.xml
			
		
		
	

	
	
		
		
	

	
	
	
		
	

	
	
		
			
		
	
	
		
		
	

 mybatis.xml

 

 



	
		
	

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
	

	
	


 struts2.xml

 

 



	
	
	
	
		
			/jsps/succ.jsp
		
	
	

 

 

4:利用mybatis-generator-core,全自动生成mapping,domain,dao。本文后面附带此工具,可进行下载使用。另外,generatorConfig.xml可参考如下。在mysql和oracle中均可使用。由于本次构建环境采用的是mysql,故将oracle的配置注释起来了

全注解实现SSM_第2张图片

注:generatorConfig.xml

 



	
	
	
	
		
			
		
		
		
		
		
		
			
		
		
		
			
			
		
		
		
			
		
		
		
			
		
		
		

除此之外,还需要oracle或者mysql的驱动包,亦可在本文后面下载

 

5:实现效果如下:
全注解实现SSM_第3张图片
 
 6:进行service的代码编写


 IUserService.java

 

package cn.wind.service;

import cn.wind.domain.User;

public interface IUserService {
	int deleteByPrimaryKey(String id);

	int insert(User record);

	int insertSelective(User record);

	User selectByPrimaryKey(String id);

	int updateByPrimaryKeySelective(User record);

	int updateByPrimaryKey(User record);
}

 UserService.java

 

 

package cn.wind.service;

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

import cn.wind.dao.UserMapper;
import cn.wind.domain.User;

// @Service("UserService")给实例化的UserService取名字为UserService
@Service("UserService")
public class UserService implements IUserService {
	// 完成自动装配的工作
	// 通过 @Autowired的使用来消除 set ,get方法。
	@Autowired
	UserMapper userMapper;

	@Override
	public int deleteByPrimaryKey(String id) {
		userMapper.deleteByPrimaryKey(id);
		return 0;
	}

	@Override
	public int insert(User record) {
		userMapper.insert(record);
		return 0;
	}

	@Override
	public int insertSelective(User record) {
		userMapper.insertSelective(record);
		return 0;
	}

	@Override
	public User selectByPrimaryKey(String id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(User record) {
		userMapper.updateByPrimaryKeySelective(record);
		return 0;
	}

	@Override
	public int updateByPrimaryKey(User record) {
		userMapper.updateByPrimaryKey(record);
		return 0;
	}
}

 7:action

 

 

package cn.wind.action;

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

import cn.wind.domain.User;
import cn.wind.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven {

	private static final long serialVersionUID = 1L;
	private User u = new User();

	@Override
	public User getModel() {
		return u;
	}

	@Autowired
	IUserService userService;

	@Override
	public String execute() throws Exception {
		User user= userService.selectByPrimaryKey("1");
		System.out.println("query success:"+user.getName()+","+user.getPwd());
		return SUCCESS;
	}
}

 

 

 

8:此时,框架搭建完毕,进行测试。

http://localhost:8080/Demo_SSM_1/user

可以在console查看到查询内容。

这只是简单demo,不再做其他扩展

  • 全注解实现SSM_第4张图片
  • 大小: 25 KB
  • MyBatis_Generator_1.3.1.zip (2.2 MB)
  • 下载次数: 11
  • mysql-connector-java-5.1.34.jar (937.9 KB)
  • 下载次数: 4
  • ojdbc14.jar (1.5 MB)
  • 下载次数: 1
  • 全注解实现SSM_第5张图片
  • 大小: 3.9 KB
  • 全注解实现SSM_第6张图片
  • 大小: 4.8 KB
  • 全注解实现SSM_第7张图片
  • 大小: 2.4 KB
  • Demo_SSM_1.rar (28.3 KB)
  • 下载次数: 16
  • 查看图片附件

你可能感兴趣的:(SSM,全注解,搭建)