SSM框架整合小结(下) spring+Mybatis

下载安装Mybatis generator插件
右键resource目录新建 other Mybatis xml
生成 generatorConfig.xml

<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- <classPathEntry  
        location="\usr\local\mysql.jar" />  -->
  <context id="context1">

    <jdbcConnection connectionURL="jdbc:mysql://121.199.29.225:3306/test?characterEncoding=UTF-8" driverClass="com.mysql.jdbc.Driver" password="klyfzx" userId="root" />
    <javaModelGenerator targetPackage="com.sun.entity" targetProject="hello/src/main/java" ></javaModelGenerator>
    <sqlMapGenerator targetPackage="com.sun.mapper" targetProject="hello/src/main/java" ></sqlMapGenerator>
    <javaClientGenerator targetPackage="com.sun.dao" targetProject="hello/src/main/java" type="XMLMAPPER" ></javaClientGenerator>
    <!-- false 是为了去掉example类 -->
    <table tableName="device" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
    enableSelectByExample="false" selectByExampleQueryId="false"/>

  </context>
</generatorConfiguration>

点到generatorConfig.xml右键生成

Unexpected error while running MyBatis Generator.
Exception getting JDBC Driver

解决办法
<classPathEntry location="//usr//local//mysql.jar" /> mac 下 windonws一样

新建 log4j.properties 注意调整编码

log4j.rootLogger=INFO,Console,File  
#定义日志输出目的地为控制台  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
#可以灵活地指定日志输出格式,下面一行是指定具体的格式  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  

#文件大小到达指定尺寸的时候产生一个新的文件  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
#指定输出目录  
log4j.appender.File.File = logs/ssm.log  
#定义文件最大大小  
log4j.appender.File.MaxFileSize = 10MB  
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志  
log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n  

新建 jdbc.properties

driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://121.199.29.225:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc_username=root
jdbc_password=klyfzx
validationQuery=SELECT 1
maxPoolSize=33
initialSize=5
maxActive=10
maxIdle=20
timeBetweenEvictionRunsMillis=60000
minIdle=5
maxWait=60000
minEvictableIdleTimeMillis=25200000
removeAbandonedTimeout=1800

新建 spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  


    <!-- 引入配置文件 -->  
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties" />  
    </bean>  

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${jdbc_url}" />  
        <property name="username" value="${jdbc_username}" />  
        <property name="password" value="${jdbc_password}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean>  
   <!-- 自动扫描 -->  
 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com/sun/mapper/*.xml"></property>  
    </bean>  

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.sun.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>  

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  

</beans> 

修改spring-mvc.xml 为下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!--开启注解处理器 -->
    <context:annotation-config />

    <context:component-scan base-package="com.sun.controller" />
    <context:component-scan base-package="com.sun.dao" />
    <context:component-scan base-package="com.sun.entity" />
    <context:component-scan base-package="com.sun.service.impl" />
    <context:component-scan base-package="com.sun.service" />
    <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

        <!-- 让dispertcher servlet 启用基于 annotation 的 HandlerMapping -->
    <mvc:annotation-driven />


    <!-- 开启组件扫描 -->
    <!-- 只搜索 @Controller 标注的类,不搜索其它标注的类 -->
    <!-- base-package: 我们将来扫描的包名(后面会建立) -->
    <context:component-scan base-package="com.sun">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>



    <!-- 指定使用 JstlView 来获取 View -->
    <bean id="defaultViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:order="3">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="contentType" value="text/html" />
        <!-- spring mvc 指定存放 jsp 的目录 -->
        <property name="prefix" value="/WEB-INF/jsps/" />
        <!-- 后缀名为 .jsp -->
        <property name="suffix" value=".jsp" />
    </bean>
    <import resource="spring-mybatis.xml"/>
</beans>

建一接口

public interface IUserService {
  boolean addUser(User user);
}

实现

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public boolean addUser(User user) {
        return userMapper.insert(user) == 1;
    }

}

Hello.java 代码更改

@Controller
@RequestMapping(value = "hello"/* ,method=RequestMethod.POST */)
public class Hello {
    @Autowired
    private IUserService userService;
    @RequestMapping(value = "login")
    public void Login(String username, String password,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        System.out.println(username + "" + password);
        response.setContentType("application/json;charset=utf-8");
        User user = new User();
        user.setName(username);
        user.setPwd(password);

        if(userService.addUser(user)){
        response.getWriter().write("1");
        }else{
        response.getWriter().write("0");    
        }
    }
}

[org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [/just/hello/login] in DispatcherServlet with name ‘springServlet’
查看是否 basePage地址正确

运行即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值