会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133499个问题
JAVA 全系列/第九阶段:SpringBoot与MybatisPlus/MybatisPlus(旧) 9976楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 9977楼

from multiprocessing import Process
from time import sleep

class Myprocess(Process):
    def __init__(self,name):#这个name就是类里面的参数
        Process.__init__(self)
        self.name=name
    def run(self):#这个self就不需要再写个namae了 因为上面有了
        print(f"Process:{self.name}start")
        
p1=Myprocess('pcl')

p1.start()

    老师 请问报错里 为什么会出现几百行的错误 我的代码都没有那么多行


C:\Users\pcl\venv\Scripts\python.exe "C:/Users/pcl/.1aPython all exercise/线程与进程/进程实现/进程类包装.py"

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 116, in spawn_main

    exitcode = _main(fd, parent_sentinel)

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 125, in _main

    prepare(preparation_data)

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 236, in prepare

    _fixup_main_from_path(data['init_main_from_path'])

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path

    main_content = runpy.run_path(main_path,

  File "C:\Users\pcl\lib\runpy.py", line 268, in run_path

    return _run_module_code(code, init_globals, run_name,

  File "C:\Users\pcl\lib\runpy.py", line 97, in _run_module_code

    _run_code(code, mod_globals, init_globals,

  File "C:\Users\pcl\lib\runpy.py", line 87, in _run_code

    exec(code, run_globals)

  File "C:\Users\pcl\.1aPython all exercise\线程与进程\进程实现\进程类包装.py", line 13, in <module>

    p1.start()

  File "C:\Users\pcl\lib\multiprocessing\process.py", line 121, in start

    self._popen = self._Popen(self)

  File "C:\Users\pcl\lib\multiprocessing\context.py", line 224, in _Popen

    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\Users\pcl\lib\multiprocessing\context.py", line 327, in _Popen

    return Popen(process_obj)

  File "C:\Users\pcl\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__

    prep_data = spawn.get_preparation_data(process_obj._name)

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 154, in get_preparation_data

    _check_not_importing_main()

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main

    raise RuntimeError('''

RuntimeError: 

        An attempt has been made to start a new process before the

        current process has finished its bootstrapping phase.


        This probably means that you are not using fork to start your

        child processes and you have forgotten to use the proper idiom

        in the main module:


            if __name__ == '__main__':

                freeze_support()

                ...


        The "freeze_support()" line can be omitted if the program

        is not going to be frozen to produce an executable.


Process finished with exit code 0


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 9978楼
JAVA 全系列/第七阶段:项目管理与SSM框架/Maven 9979楼

<?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 9980楼
JAVA 全系列/第十六阶段:全文检索与日志管理/Elasticsearch旧 9983楼
JAVA 全系列/第十二阶段:消息中间件与高并发处理/Nginx 9986楼
WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/Ego商城高级Vue实战项目 9987楼

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 基础深化和提高/容器(旧) 9988楼
JAVA 全系列/第十一阶段:智能家居项目(旧)/至尊智能家居第二天 9990楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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