SSM整合之注解式开发

SSM项目结构图
SSM整合之注解式开发_第1张图片

Maven依赖

    
    
        junit
        junit
        3.8.1
        test
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
    
    
    
        org.ow2.asm
        asm
        4.2
    
    
        cglib
        cglib
        3.1
    
    
        commons-logging
        commons-logging
        1.2
    
    
        log4j
        log4j
        1.2.17
    
    
        org.apache.logging.log4j
        log4j-api
        2.2
    
    
        org.apache.logging.log4j
        log4j-core
        2.2
    
    
        org.mybatis
        mybatis
        3.3.0
    
    
        org.slf4j
        slf4j-api
        1.7.12
    
    
        org.slf4j
        slf4j-log4j12
        1.7.12
        test
    
    
    
        aopalliance
        aopalliance
        1.0
    
    
        org.aspectj
        aspectjweaver
        1.6.8
    
    
        org.springframework
        spring-aop
        4.2.1.RELEASE
    
    
        org.springframework
        spring-aspects
        4.2.1.RELEASE
    
    
        org.springframework
        spring-beans
        4.2.1.RELEASE
    
    
        org.springframework
        spring-context
        4.2.1.RELEASE
    
    
        org.springframework
        spring-context-support
        4.2.1.RELEASE
    
    
        org.springframework
        spring-core
        4.2.1.RELEASE
    
    
        org.springframework
        spring-expression
        4.2.1.RELEASE
    
    
        org.springframework
        spring-jdbc
        4.2.1.RELEASE
    
    
        org.springframework
        spring-orm
        4.2.1.RELEASE
    
    
        org.springframework
        spring-tx
        4.2.1.RELEASE
    
    
        org.springframework
        spring-web
        4.2.1.RELEASE
    
    
        org.springframework
        spring-webmvc
        4.2.1.RELEASE
    
    
    
        org.mybatis
        mybatis-spring
        1.2.3
    
    
    
        mysql
        mysql-connector-java
        5.1.24
    
    
    
        com.mchange
        c3p0
        0.9.5.2
    

Spring配置文件的命名空间



Mapper映射文件的命名空间



web.xml




    
    
        contextConfigLocation
        classpath:spring/spring-*.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            
            classpath:spring/spring-mvc.xml
        
        1
        true
    
    
        SpringMVC
        *.do
    

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

    
    
        jsp/index.jsp
    

spring-mybatis.xml


    
    
        
        
        
        
    
    
    

    
    
        
        
        
        
        
        
        
            
                true
            
        
    

    
    
        
        
        
    

spring-tx.xml


    
    
        
    
    
    

spring-mvc.xml


    
    

spring-service.xml


    
    

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssmdemo?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

user.xml


    
        INSERT INTO t_user(nickname, age) VALUES (#{nickname}, #{age})
    

实例代码

public class User {
    private Long id;
    private String nickname;
    private Integer age;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

@Controller
public class UserController{
    @Autowired
    private IUserService service;

    @RequestMapping(value = "/register.do", method = RequestMethod.POST)
    public ModelAndView doRegister(User user) {
        service.addUser(user);

        return new ModelAndView("/jsp/show.jsp");
    }
}

public interface IUserService {
    void addUser(User user);
}
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao dao;

    @Transactional
    public void addUser(User user) {
        dao.insertUser(user);
    }
}

public interface IUserDao {
    void insertUser(User user);
}

//index.jsp


    
姓名:
年龄:

你可能感兴趣的:(Spring笔记)