没打开tomcat时候的dao使用

本文介绍了如何使用Spring框架通过XML文件配置ApplicationContext来实例化UserDaoImplForOrc类,并完成了一个简单的添加用户操作。文章展示了如何创建ApplicationContext,获取Bean并进行业务逻辑处理。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		UserDaoImplForOrc dao = (UserDaoImplForOrc) applicationContext.getBean("userDaoImplForOrc");//类上面的注解名字
		User user = new User();
		user.setName("wang");
		user.setCount(1000);
		dao.add(user);


ApplicationContext applicationContext = new FileSystemXmlApplicationContext(new String[]{}//这个是可以使用绝对路径的。


### **在 IntelliJ IDEA 中使用 Tomcat 插件运行 SSH(Struts2 + Spring + Hibernate)项目** 如果你的项目是基于 **SSH(Struts2 + Spring + Hibernate)** 框架的,并且希望使用 **IntelliJ IDEA** 的 **Tomcat 插件** 运行,可以按照以下步骤配置: --- ## **1. 确保项目结构正确** SSH 项目通常包含: - **Struts2**(Web 层) - **Spring**(业务层) - **Hibernate**(持久层) - **Web 配置文件**(`web.xml`、`struts.xml`、`applicationContext.xml` 等) 项目目录结构示例: ``` src/ ├── main/ │ ├── java/ # Java 代码(Action、Service、DAO) │ ├── resources/ # 配置文件(struts.xml、hibernate.cfg.xml) │ └── webapp/ # Web 资源(JSP、WEB-INF/web.xml) pom.xml # Maven 依赖管理 ``` --- ## **2. 配置 Tomcat 插件** ### **(1) 确保已安装 Tomcat** - 下载 Tomcat(推荐 **Tomcat 8.5.x** 或 **9.x**):[Apache Tomcat 官网](https://tomcat.apache.org/) - 解压到本地目录(如 `C:\apache-tomcat-9.0.xx`) ### **(2) 在 IDEA 中配置 Tomcat** 1. **打开 `Run/Debug Configurations`**: - 点击 IDEA 右上角 **`Edit Configurations`**(或 `Run` → `Edit Configurations`)。 2. **添加 Tomcat Server**: - 点击 **`+`** → **`Tomcat Server`** → **`Local`**。 3. **配置 Tomcat 路径**: - 在 `Application Server` 中选择 Tomcat 安装目录(如 `C:\apache-tomcat-9.0.xx`)。 4. **配置 Deployment(部署)**: - 点击 **`Deployment`** 选项卡 → **`+`** → **`Artifact`**。 - 选择你的 **`war` 或 `war exploded`** 项目(推荐 `war exploded` 以支持热部署)。 5. **配置 Context Path**(可选): - 默认访问路径是 `http://localhost:8080/your-project/`,可以修改为 `/`(根路径)。 --- ## **3. 解决 SSH 依赖问题** SSH 项目通常需要以下依赖(Maven `pom.xml`): ```xml <!-- Struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.30</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.18</version> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.6.5.Final</version> </dependency> ``` **检查依赖冲突**: - 运行 `mvn dependency:tree` 查看是否有版本冲突(如 `asm`、`cglib` 等)。 - 使用 `<exclusions>` 排除冲突的依赖。 --- ## **4. 检查 Web 配置** ### **(1) `web.xml` 配置** 确保 `web.xml` 包含 **Struts2 Filter** 和 **Spring Listener**: ```xml <!-- Struts2 Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring Context Listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> ``` ### **(2) `struts.xml` 配置** 确保 `struts.xml` 正确配置 Action: ```xml <struts> <package name="default" extends="struts-default"> <action name="hello" class="com.example.action.HelloAction"> <result>/hello.jsp</result> </action> </package> </struts> ``` ### **(3) `applicationContext.xml` 配置** 确保 Spring 正确加载 Hibernate 和 Service: ```xml <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/your_db"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> ``` --- ## **5. 运行项目** 1. **构建项目**: - 执行 `mvn clean install` 确保编译通过。 2. **启动 Tomcat**: - 在 IDEA 中选择配置好的 Tomcat,点击 **`Run`** 或 **`Debug`**。 3. **访问测试**: - 打开浏览器访问 `http://localhost:8080/your-project/hello.action`(根据 `struts.xml` 配置的路径)。 --- ## **6. 常见问题排查** | **问题** | **可能原因** | **解决方案** | |----------|------------|------------| | **404 错误** | `struts2` 过滤器未生效 | 检查 `web.xml` 是否配置 `StrutsPrepareAndExecuteFilter` | | **Spring Bean 加载失败** | `applicationContext.xml` 路径错误 | 确保 `contextConfigLocation` 指向正确的配置文件 | | **Hibernate 报错** | 数据库连接失败 | 检查 `dataSource` 配置、数据库是否启动 | | **依赖冲突** | 多个版本的 `asm`/`cglib` | 使用 `mvn dependency:tree` 排查冲突 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值