会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132884个问题

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd ">

    <!--配置解析properties工具类-->
    <context:property-placeholder location="db.properties"/>

    <!--配置数据源-->
    <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource1"/>
        <!--配置别名-->
       <property name="transactionFactory" value="com.cxx.pojo"/>
        <!--引入mapper的配置文件-->
        <property name="mapperLocations" value="com/cxx/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate-->
    <bean id="sqlSessionTemplate1" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>
    <!--配置持久层Bean对象-->
    <bean id="userDao1" class="com.cxx.dao.impl.UsersDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate1"/>
    </bean>
    <!--配置业务层bean对象-->
    <bean id="usersService" class="com.cxx.service.impl.UsersServiceImpl">
        <property name="usersDao" ref="userDao1"/>
    </bean>
</beans>

持久层如下:

package com.cxx.dao;

import com.cxx.pojo.Users;

public interface UsersDao {
    //添加用户的持久层接口方法
    void addUsers(Users users);
}

持久层实现类

package com.cxx.dao.impl;

import com.cxx.dao.UsersDao;
import com.cxx.mapper.UsersMapper;
import com.cxx.pojo.Users;
import org.mybatis.spring.SqlSessionTemplate;

public class UsersDaoImpl implements UsersDao {
    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    //添加用户的持久层接口实现类的方法
    @Override
    public void addUsers(Users users) {
        UsersMapper mapper = this.sqlSessionTemplate.getMapper(UsersMapper.class);
        mapper.insertSelective(users);
    }
}


业务层接口

package com.cxx.service;

import com.cxx.pojo.Users;

public interface UsersService {
    void  insertUsers(Users users);
}


业务层接口实现类

package com.cxx.service.impl;

import com.cxx.dao.UsersDao;
import com.cxx.pojo.Users;
import com.cxx.service.UsersService;


public class UsersServiceImpl implements UsersService {
    private UsersDao usersDao;

    public UsersDao getUsersDao() {
        return usersDao;
    }

    public void setUsersDao(UsersDao usersDao) {
        this.usersDao = usersDao;
    }

    @Override
    public void insertUsers(Users users) {
        this.usersDao.addUsers(users);
    }
}


测试类

package com.cxx.test;

import com.cxx.pojo.Users;
import com.cxx.service.UsersService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AddUsersTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");
        Users users = new Users();
        users.setUsername("老吴");
        users.setUsersex("男");
        usersService.insertUsers(users);
    }
}


这里跟视频做的不一样,持久层和业务层分离做的,,,但是运行报错:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactoryBean' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.apache.ibatis.transaction.TransactionFactory' for property 'transactionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.apache.ibatis.transaction.TransactionFactory' for property 'transactionFactory': no matching editors or conversion strategy found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:610)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:925)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.cxx.test.AddUsersTest.main(AddUsersTest.java:10)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.apache.ibatis.transaction.TransactionFactory' for property 'transactionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.apache.ibatis.transaction.TransactionFactory' for property 'transactionFactory': no matching editors or conversion strategy found
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:595)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:609)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:219)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1738)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1694)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1434)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601)
	... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.apache.ibatis.transaction.TransactionFactory' for property 'transactionFactory': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
	... 17 more

Process finished with exit code 1


分析了半小时左右,去掉了spring配置文件中的配置别名 运行成功,为什么这里别名会引起报错??

<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource1"/>
    <!--配置别名-->
   <!--<property name="transactionFactory" value="com.cxx.pojo"/>-->
    <!--引入mapper的配置文件-->
    <property name="mapperLocations" value="com/cxx/mapper/*.xml"/>
</bean>


去掉反而正常了呢?

JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 9961楼
JAVA 全系列/第十五阶段:全文检索与日志管理/Elasticsearch旧 9964楼
JAVA 全系列/第十一阶段:消息中间件与高并发处理/Nginx 9967楼
WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/Ego商城高级Vue实战项目 9968楼

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;


/*  元素自身实现比较规则  */
public class TestTreeMap {

    public static void main(String[] args) {
        //实例化TreeMap
        Map<Users,String> map = new TreeMap<>();
        Users u1 = new Users("oldlu",18);
        Users u2 = new Users("admin",22);
        Users u3 = new Users("sxt",22);

        // 添加
        map.put(u1,"oldlu");
        map.put(u2,"admin");
        map.put(u3,"sxt");
        System.out.println(map.get(u3));

        // 遍历
        Set<Users> set = map.keySet();
        for(Users users:set){
            Users key = users;
            String val = map.get(key);
            System.out.println(key+" --- "+val);
        }

    }

}

class Users implements Comparable<Users>{
    private String usersName;
    private int usersAge;

    public Users() {
    }

    public Users(String usersName, int usersAge) {
        this.usersName = usersName;
        this.usersAge = usersAge;
    }

    public String getUsersName() {
        return usersName;
    }

    public void setUsersName(String usersName) {
        this.usersName = usersName;
    }

    public int getUsersAge() {
        return usersAge;
    }

    public void setUsersAge(int usersAge) {
        this.usersAge = usersAge;
    }

    @Override
    public String toString() {
        return "姓名:" + usersName + "    年龄:" + usersAge;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Users users = (Users) o;
        return usersAge == users.usersAge &&
                Objects.equals(usersName, users.usersName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(usersName, usersAge);
    }

    @Override
    public int compareTo(Users o) {
        // 比较年龄 由小到大
        if(this.usersAge < o.getUsersAge()){
            return 1;
        }
        // 如果年龄相等 按名字排序
        if(this.usersAge == o.getUsersAge()){
            this.usersName.compareTo(o.getUsersName());
        }
        return -1;
    }
}

image.png

value值为什么是null?


JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 9969楼
JAVA 全系列/第十一阶段:智能家居项目(旧)/至尊智能家居第二天 9971楼
Python 全系列/第一阶段:Python入门/编程基本概念 9972楼

package G_Multithreading.E_Synchronized;

//面包类
class Bread{
    private int id;
    public Bread(int id){
        this.id = id;
    }
}

//缓冲区
class SynBuffer{
    //存放面包的盒子
    private Bread[] breads = new Bread[10];
    //存放面包盒子的索引
    private int index;
    //放面包
    public synchronized void push(Bread bread){
        //判断盒子是否存满
        while (breads.length == this.index + 1){
            try {
                /*
                语法:wait(),该方法必须要在synchronized块中调用。
                     wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
                     其他需要该对象锁的线程就可以继续运行了。
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /*
            语法:该方法必须要在synchronized块中调用。
                 该方法会唤醒处于相同对象的,并且等待状态队列中的一个线程。
             */
            //提醒取面包
            this.notify();
            breads[this.index] = bread;
            this.index++;
        }
    }
    //取走面包
    public synchronized Bread pop(){
        while (breads.length == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return breads[this.index];
    }
}

//生产面包线程
class Produce extends Thread{
    SynBuffer synBuffer;
    public Produce(SynBuffer synBuffer){
        this.synBuffer = synBuffer;
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++){
            Bread bread = new Bread(i);
            this.synBuffer.push(bread);
            System.out.println("生产面包" + i);
        }
    }
}

//消费者线程
class Customer extends Thread{
    SynBuffer synBuffer;
    public Customer(SynBuffer synBuffer){
        this.synBuffer = synBuffer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++){
            Bread bread = this.synBuffer.pop();
            System.out.println("取走面包" + i);
        }
    }
}
public class SynchronizedBuffer {
    public static void main(String[] args) {
        SynBuffer synBuffer = new SynBuffer();
        new Produce(synBuffer).start();
        new Customer(synBuffer).start();
    }
}

老师,他到取出的是够就高速index是-1,哪里出错了?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 9973楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 9974楼
JAVA 全系列/第九阶段:权限控制与安全认证/Spring Security(旧) 9975楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637