SSM框架作業(yè)代碼:查詢功能,數(shù)據(jù)庫插入語句時記得提交事務(wù),個人學(xué)術(shù)【詩書畫唱】
概括:
在使用SSM框架查詢數(shù)據(jù)的時候必須要注意的事務(wù)相關(guān)的事項?
Oracle數(shù)據(jù)庫代碼
?eclipse中的項目代碼
訪問路徑運行效果
SSM框架必備的要導(dǎo)入項目的包
PS:淦!之前用SSM框架查詢數(shù)據(jù)庫的內(nèi)容,我嘗試很多遍,看控制臺打印的日志等等都沒報錯,代碼也沒錯等等,納悶了很久,結(jié)果寫這篇專欄的今天才發(fā)現(xiàn)原來是我新建表的時候,使用insert語句插入數(shù)據(jù)時忘記提交事務(wù)了!淦!(╬▔皿▔)凸
個人學(xué)術(shù)想法:其實如何判斷你會運用你記錄的例子呢?就是看你知道如何根據(jù)需求知道哪里要改,知道怎么改。其實平時記錄要會運用的例子,要記錄如何根據(jù)需求知道哪里要改,知道怎么改等的內(nèi)容就可以解決記憶等的問題,記錄可以解決記憶,部分思考或理解等方面的問題。比如記錄優(yōu)秀up主等的視頻例子和記錄如何根據(jù)需求知道哪里要改,知道怎么改等的內(nèi)容。比如記錄自己等人寫的項目例子和記錄項目的個人使用和哪里要注意更改和別忘了等等的注釋等等。
以上就是我很寶貴的方法!換做別人可能不愿意分享這種寶貴的方法,但因為我還有現(xiàn)存或?qū)a(chǎn)出的這種寶貴的方法,所以我暫時愿意分享這類寶貴的方法!求三連關(guān)注?。 盎甑眰?!(╬▔皿▔)凸(最近在復(fù)習(xí)《銀魂》來回想我以前看《銀魂》的美好時光!)

在使用SSM框架查詢數(shù)據(jù)的時候必須要注意的事務(wù)相關(guān)的事項 START
Oracle數(shù)據(jù)庫的表的數(shù)據(jù)插入時必須要按綠色的提交事務(wù)按鈕,不然只是暫時的數(shù)據(jù)插入,以后重啟Oracle數(shù)據(jù)庫后,這些沒有插入數(shù)據(jù)的內(nèi)容就會不見。



在使用SSM框架查詢數(shù)據(jù)的時候必須要注意的事務(wù)相關(guān)的事項?
END
用SSM框架實現(xiàn)查詢功能 START
SSM框架必備的要導(dǎo)入項目的包:


Oracle數(shù)據(jù)庫代碼(使用insert語句插入數(shù)據(jù)后記得提交事務(wù)??!淦!(╬▔皿▔)凸):
Product簡易版:
--drop table Product? ? ??
create table Product(
? ? id number primary key,
? ? name varchar2(30) not null,
? ?price? number(10,2)
?
);
--drop sequence seq_Product
create sequence seq_Product
start with 1? ? ? ?--起始值是1
increment by 1? ? ?--增長的值? ?
maxvalue 999999999 --序列號的最大值
minvalue 1? ? ? ? ?--序列號的最小值
nocycle? ? ? ? ? ? --是否循環(huán)
cache 10;? ? ? ? ? --預(yù)存
insert into Product values(seq_Product.nextval,'黑筆',1.5);
insert into Product values(seq_Product.nextval,'紅書',2.0);
insert into Product values(seq_Product.nextval,'掛面',3.0);
insert into Product values(seq_Product.nextval,'藍筆',1.5);
insert into Product values(seq_Product.nextval,'黃書',2.0);
insert into Product values(seq_Product.nextval,'拉面',3.0);
--select * from Product
?eclipse中的項目代碼:

package com.SSHC.bean;
public class Product {
? ? private Integer id;
? ? private String name;
private Double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}


package com.SSHC.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.SSHC.service.PubService;
@Controller
public class ProductController {
@Resource
private PubService pubService;
//http://localhost:8080/SSMhomework1/test
@RequestMapping("test")
? ? public String hello(){
pubService.testSv();
? ? return "test";
? ? }
}


package com.SSHC.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.SSHC.bean.Product;
@Repository
public interface ProductDao {
? ? List<Product> selectAll();
? ??
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
? ? PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"??
? ? "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace不能亂寫,必須寫成ProductDao接口的全路徑 -->
<mapper namespace="com.SSHC.dao.ProductDao">
? ? <resultMap type="Product" id="rmProduct">
? ? ? ? <id property="id" column="id"/>
? ? <result property="name" column="name"/>
? ? <result property="price" column="price"/>
? ?
? ? </resultMap>?
? ? <select id="selectAll" resultMap="rmProduct">
? ? ? ? select * from Product
? ? </select>??
? ?
? ??
</mapper>


package com.SSHC.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.SSHC.bean.Product;
import com.SSHC.dao.ProductDao;
@Service
public class PubService {
//屬性名就是接口名的首字母改成小寫
@Resource
? ? private ProductDao productDao;
? ? public void testSv(){
? ? List<Product>list = productDao.selectAll();
? ? for(Product u : list) {
? ? System.out.println("編號:"+u.getId()
? ? +"? 商品名稱:"+u.getName()
? ? +"? 商品價格"+u.getPrice());
? ? }
? ? }
}


oracle_drivername=oracle.jdbc.driver.OracleDriver
oracle_url=jdbc:oracle:thin:@localhost:1521:orcl
oracle_username=X
oracle_password=sshcPwd


log4j.rootLogger=DEBUG,Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d[%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC?
"-//mybatis.org//DTD Config 3.0//EN"??
? ? "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>??
? ? <typeAliases>
? ? <!-- Aliases:別名[?e?l??s?z]?
? ??
configuration
英[k?n?f?ɡ??re??n]
美[k?n?f?ɡj??re??n]
n. 布局; 結(jié)構(gòu); 【構(gòu)造】;?
格局; 形狀; (計算機的)配置
注:【】中的內(nèi)容是我認為有目前我要知道的重要意思之一。-->
? ? ? ? <package name="com.SSHC.bean"/>
? ? </typeAliases>
</configuration>


<?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"
? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? xmlns:tx="http://www.springframework.org/schema/tx"
? ? 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-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
? ? ? ? http://www.springframework.org/schema/tx?
? ? ? ? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
? ??
? ? <!-- 引入db.properties文件 -->
? ? <bean id="propertyConfigurer"?
? ? ? ? class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
? ? ? ? <property name="location" value="classpath:db.properties"/>
? ? </bean>
? ? <!--數(shù)據(jù)庫連接池配置-->
? ? <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"?
? ? ? ? destroy-method="close">??
? ? ? ? <property name="driverClassName" value="${oracle_drivername}"/>
? ? ? ? <property name="url" value="${oracle_url}"/>
? ? ? ? <property name="username" value="${oracle_username}"/>
? ? ? ? <property name="password" value="${oracle_password}"/>
? ? </bean>
? ? <!-- 創(chuàng)建sqlSessionFactory對象 -->
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!-- 指定數(shù)據(jù)源 -->
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <!-- 指定mybatis框架主配置文件的位置 -->
? ? ? ? <property name="configLocation" value="classpath:mybatis.xml"/>
? ? ? ? <!-- 自動掃描mapping.xml文件,**表示迭代查找 ,,也可在mybatis.xml中單獨指定xml文件 -->
? ? ? ? <property name="mapperLocations" value="classpath:com/SSHC/dao/*.xml"/>
? ? </bean>?
? ? <!-- 自動掃描com/SSHC/dao下的所有dao接口,并實現(xiàn)這些接口,
? ? ? ? ? ? ? ? ?可直接在程序中使用dao接口,不用再獲取sqlsession對象 -->
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <!-- basePackage 屬性是映射器接口文件的包路徑。
? ? ? ? ? ? ? ? ? ? ? ? 你可以使用分號或逗號 作為分隔符設(shè)置多于一個的包路徑-->
? ? ? ? <property name="basePackage" value="com/SSHC/dao"/>
? ? ? ? <!-- 因為會自動裝配 SqlSessionFactory和SqlSessionTemplate
? ? ? ? ? ? ? ? ? ? ? ? 所以沒有必要去指定SqlSessionFactory或 SqlSessionTemplate
? ? ? ? ? ? ? ? ? ? ? ? 因此可省略不配置;
? ? ? ? ? ? ? ? ? ? ? ? 但是,如果你使用了一個以上的 DataSource,那么自動裝配可能會失效。
? ? ? ? ? ? ? ? ? ? ? ? 這種情況下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName屬性
? ? ? ? ? ? ? ? ? ? ? ? 來設(shè)置正確的 bean名稱來使用 -->
? ? ? ? ?<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
? ? </bean>
</beans>


<?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"
? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? 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-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">? ? ? ? ? ? ? ? ? ? ? ?
? ? <!-- 掃描@Controller注解 -->
? ? <context:component-scan base-package="com.SSHC.dao,com.SSHC.service,com.SSHC.controller"/>
? ? <!-- 默認注冊RequestMappingHandlerMapping和RequestMappingHandlerAdapter類 -->
? ? <mvc:annotation-driven />
? ? <!-- jsp引用外部js,css等靜態(tài)資源的解決方法(和上面的標(biāo)簽必須同時出現(xiàn),否則無法訪問url) -->
? ? <mvc:default-servlet-handler />
? ? <!-- 配置視圖名稱解析器 -->
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"?
? ? ? ? ? ? id="internalResourceViewResolver">
? ? ? ? <!-- 前綴 -->
? ? ? ? <!-- 將所有的jsp文件存放在/WEB-INF/my/目錄下 -->
? ? ? ? <property name="prefix" value="/WEB-INF/" />
? ? ? ? <!-- 后綴 -->
? ? ? ? <property name="suffix" value=".jsp" />
? ? ? ? <!-- 優(yōu)先級設(shè)定 -->
? ? ? ? <property name="order" value="10"></property>
? ? </bean>??
</beans>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>測試成功</h1>
? ? </body>
</html>


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
? <display-name>SSMhomework1</display-name>
? <!-- springcore框架配置 -->
? <listener>
? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>
? <!-- controller中文亂碼處理,注意一點:要配置在所有過濾器的前面 -->
? <filter>
? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
? ? <init-param>
? ? ? <param-name>encoding</param-name>
? ? ? <param-value>utf-8</param-value>
? ? </init-param>
? </filter>
? <filter-mapping>
? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? <url-pattern>/*</url-pattern>
? </filter-mapping>
? <!-- springmvc框架配置 -->
? <servlet>
? ? ? <servlet-name>springmvc</servlet-name>
? ? ? <servlet-class>
? ? ? ? ? org.springframework.web.servlet.DispatcherServlet
? ? ? </servlet-class>
? ? ? <load-on-startup>1</load-on-startup>
? </servlet>
? <servlet-mapping>
? ? ? <servlet-name>springmvc</servlet-name>
? ? ? <url-pattern>/</url-pattern>
? </servlet-mapping>
? <welcome-file-list>
? ? <welcome-file>index.html</welcome-file>
? ? <welcome-file>index.htm</welcome-file>
? ? <welcome-file>index.jsp</welcome-file>
? ? <welcome-file>default.html</welcome-file>
? ? <welcome-file>default.htm</welcome-file>
? ? <welcome-file>default.jsp</welcome-file>
? </welcome-file-list>
</web-app>


http://localhost:8080/SSMhomework1/test

訪問路徑運行效果:
http://localhost:8080/SSMhomework1/test

運用這個項目時大部分情況要改的部分:

