Spring环境配置

                         idea---Spring环境配置

步骤:

1,在idea里面new一个Project,在里面找到Maven项目,点next

2,在这里填上公司名和项目名,在点next,再点finish就创建成功了.

3,接下来就是项目的环境配置了

在pom.xml文件里加上我们需要用到的一些jar包.

你的电脑里面有一个maven仓库,如果你的maven仓库里没有这些jar包,它就会从网上下载,到你的maven仓库里

4,加入依赖之后,开始编写spring的配置文件

创建一个spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

在beans里面加入bean来定义要控制反转的类

 <!--受spring容器管理类, 使用<bean>标签管理

    id="唯一标识"
    -->

    <bean id="aaa" class="com.westos.dao.UserDao"
        init-method="test1" destroy-method="test2">

注意:
bean id是严格区分大小写的 如果id不存在会 NoSuchBeanDefinitionException

如果容器中同一个类型的bean有多个,再根据类型获取的话 NoUniqueBeanDefinitionException

5,连接数据库,整合mabatis

创建jdbc.properties文件在里面写好连接数据库需要用的代码

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eshop?serverTimezone=GMT%2B8&useSSL=false&useServerPrepStmts=true&cachePrepStmts=true&rewriteBatchedStatements=true&useCursorFetch=true&defaultFetchSize=100&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=root
jdbc.max=10
jdbc.min=2
<!--读取properties 文件location="文件位置"-->
<context:property-placeholder location="classpath:jdbc.properties"/>

然后把数据源对象交给Spring容器管理

这样spring的环境配置就基本完成了.

猜你喜欢

转载自blog.csdn.net/qq_42541456/article/details/83619206
今日推荐