ssm

mybatis逆向工程

逆向工程配置文件

generatorConfig.xml文件





    
        
        
            
        

        
        
        

        
        
            
        

        
        
            
            
            
            
        

        
        
            
            
        

        
        
            
            
        

        
        
        

逆向工程代码

public class GeneratorSqlmap {
    public void generator() throws Exception {
        List warnings = new ArrayList();
        boolean overwrite = true;
        // 指定逆向工程配置文件
        File configFile = new File("generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试类(可以在ssm整合的时候测试)

    public static void main(String[] args) throws IOException {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession session = factory.openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        UserExample example=new UserExample();
        example.createCriteria().andUserNameEqualTo("dev2").andUserPwdEqualTo("123");
        List list = mapper.selectByExample(example);
        System.out.println(list);
    }

spring-mybatis整合

目录结构

ssm_第1张图片
1.png

1、spring-mybatis整合xml文件

applicationContext-mybaties.xml




          
    
    
        
        
        
        
         
    
    
        
        
        
        
        
        
        
        
    
     
    
        
        
        
        
    

2、mybatis-config.xml




  
    
                  
    
  

3、db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/appmanager?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=
jdbc.acquireIncrement=3
jdbc.initialPoolSize=10
jdbc.minPoolSize=2
jdbc.maxPoolSize=200
jdbc.maxIdleTime=1000

4、log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.hemi.controller=DEBUG
log4j.logger.com.hemi.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

spring事务配置

applicationContext-tx.xml




    
        
    

    
        
    

    
        
            
            
            
            
        
    

springMVC

web.xml


    dispatherServlet
    org.springframework.web.servlet.DispatcherServlet    
    
        contextConfigLocation
        classpath:springmvc.xml
    
    1
  
  
    dispatherServlet
    /
  

json数据发送

@RequestBody jackson的3个jar包

    @RequestMapping("jsonToBean")
    @ResponseBody
    public User jsonToBean(@RequestBody User user){
        return user;
    }

contentType:appliation/json;charset=utf-8 JSON.stringify(json),

    var json={username:"lisi",password:"123"};//定义json对象

    $(function(){
        $("#btn").click(function(){
            $.ajax({
                url:"/springmvc/jsonToBean",
                type:"post",
                contentType:"application/json;charset=utf-8",
                //js原生的一个将json对象转成json字符串的工具类           
                data:JSON.stringify(json),
                dataType:"text",
                success:function(data){
                    $("#content").html(data);
                }
            });
        });
    });

你可能感兴趣的:(ssm)