IDEA2018 Mybatis4.2

(1)无法注入Bean

这里有一个记录,但是没解决我的问题:Spring NoSuchBeanDefinitionException六大原因总结。其中第一个bean not found,@autowired失败。各种查之后应该是没有扫描到。

也就是 context:component-scan base-package="Service"/

这里必须

 

https://blog.csdn.net/free4294/article/details/38706569

上传一下springcfg.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <!--启用扫描机智,并指定扫描对应的包-->
    <context:annotation-config/>
    <context:component-scan base-package="Services"/>
    <!--数据库连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
        <property name="maxActive" value="255"/>
        <property name="maxIdle" value="5"/>
        <property name="maxWait" value="10000"/>
    </bean>

    <!--集成MyBatis-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--指定MyBatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>


<!--
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
-->



       <!--数据库事务管理器-->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
     </bean>

    <!--使用注解定义事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--采用自动扫描方式创建mapper bean-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="Services,Mapper,POJO"/>
        <property name="SqlSessionFactory" ref="SqlSessionFactory"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>



</beans>

我的问题是路径问题.

(2):MYSQL 设置自增

 List<Role> roleList=new ArrayList<Role>();
        for (int i = 0; i <2 ; i++) {
            Role role=new Role();
            role.setRoleName("role_name_11");
            role.setNote("note_111");
            roleList.add(role);

这里没有设置role.setId();

所以在Mysql中,id必须为int型,且点击Auto Icrement按钮。id点击AI按钮。

方法如下:https://www.cnblogs.com/tekkaman/p/6728632.html