java學(xué)習(xí)

          java學(xué)習(xí)

           

          java的值傳遞和引用傳遞

          public class Man {
          private String id;
            public String getId() {
          return id;
          }
          public void setId(String id) {
          this.id = id;
          }
          public void man() {
          System.out.println("man");
          }
          }
          public class TestMan {
          //值傳遞傳遞的是值的副本
          public static int getInt(int i){
          i=4;
          return i;
          }
          //引用傳遞,傳遞的是對(duì)象的引用,指向的還是原來(lái)的對(duì)象
          public static Man getMan(Man man){
          man.setId("111");
          return man;
          }
          public static String getS(String s){
          return s="111";
          }
          public static void main(String[] args) {
          int i=1;
          TestMan.getInt(i);
          System.out.println(i);
          Man man = new Man();
          man.setId("222");
          TestMan.getMan(man);
          System.out.println(man.getId());
          String s="222";
          TestMan.getS(s);
          System.out.println(s);
          }
          }

          posted @ 2017-12-08 17:19 楊軍威 閱讀(135) | 評(píng)論 (0)編輯 收藏

          子線(xiàn)程執(zhí)行10次,主線(xiàn)程執(zhí)行100次,然后再子線(xiàn)程執(zhí)行10次,主線(xiàn)程執(zhí)行100次,循環(huán)50次

          package com.yjw.thread;
          public class Test2 {
          static class Businis {
          boolean f = false;// true sub執(zhí)行,false main執(zhí)行
          public synchronized void sub(int i) {
          if (!f) {
          try {
          this.wait();
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          for (int j = 1; j <= 10; j++) {
          System.out.println("sub=" + j + ",loop=" + i);
          }
          f = false;
          this.notify();
          }
          public synchronized void main(int i) {
          if (f) {
          try {
          this.wait();
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          for (int j = 1; j <= 100; j++) {
          System.out.println("main=" + j + ",loop=" + i);
          }
          f = true;
          this.notify();
          }
          }
          public static void main(String[] args) {
          final Businis businis = new Businis();
          for (int i = 1; i <= 50; i++) {
          businis.main(i);
                                  businis.sub(i);
          }
          }
          }

          posted @ 2017-12-08 16:24 楊軍威 閱讀(250) | 評(píng)論 (0)編輯 收藏

          線(xiàn)程重寫(xiě)了run方法并傳入一個(gè)新的線(xiàn)程時(shí)執(zhí)行run的改變

          @org.junit.Test
          public void test2(){
          new Thread( new Runnable() {
          public void run() {
          System.out.println(2);
          }
          }){
          public void run() {
          System.out.println(1);
          };
          }.start();
          }
          線(xiàn)程重寫(xiě)了run方法,又傳入了新的線(xiàn)程,此線(xiàn)程只執(zhí)行重寫(xiě)了run方法的方法,因?yàn)楦鶕?jù)面向?qū)ο蟮脑瓌t,子類(lèi)重寫(xiě)了父類(lèi)的方法,只執(zhí)行子類(lèi)的方法。

          posted @ 2017-12-08 09:39 楊軍威 閱讀(243) | 評(píng)論 (0)編輯 收藏

          瀏覽器觸發(fā)ajax請(qǐng)求的次數(shù)限制

          一個(gè)頁(yè)面同時(shí)觸發(fā)ajax請(qǐng)求的次數(shù)不能過(guò)多,同時(shí)觸發(fā)的次數(shù)不要超過(guò)6個(gè),不然過(guò)多的ajax會(huì)被瀏覽器阻塞,不能實(shí)現(xiàn)異步請(qǐng)求。

          posted @ 2017-12-06 14:56 楊軍威 閱讀(366) | 評(píng)論 (0)編輯 收藏

          負(fù)載均衡出現(xiàn)404

          負(fù)載均衡時(shí),如果客戶(hù)端訪(fǎng)問(wèn)出現(xiàn)404的情況時(shí),有可能是負(fù)載均衡映射到了空的服務(wù)造成的,需要檢查負(fù)載均衡對(duì)應(yīng)的后臺(tái)主機(jī)是否存在。

          posted @ 2017-12-06 14:54 楊軍威 閱讀(180) | 評(píng)論 (0)編輯 收藏

          oracle建立普通用戶(hù)

          1、
          1. create user aaaaidentified by 123aaa;  
            2、
            grant connect, resource to aaaa;
            3、
            grant create session to aaaa;

          posted @ 2017-11-27 14:23 楊軍威 閱讀(161) | 評(píng)論 (0)編輯 收藏

          createQuery is not valid without active transaction

          在DAO文件中,通過(guò)sessionFactory.getCurrentSession()來(lái)獲取會(huì)話(huà),報(bào)異常:org.hibernate.HibernateException: createQuery is not valid without active transaction。經(jīng)過(guò)實(shí)驗(yàn),發(fā)現(xiàn)將Hibernate的配置文件中的<property name="current_session_context_class">thread</property>屬性去掉就好了。原來(lái)"current_session_context_class"屬性的意思是,設(shè)置當(dāng)前會(huì)話(huà)的上下文環(huán)境,如果設(shè)置為thread,那么同一線(xiàn)程則共享同一session會(huì)話(huà)。因此通過(guò)getCurrentSession()得到的session,是同一線(xiàn)程上的session,而不是Spring管理的那個(gè)能夠自動(dòng)開(kāi)啟事務(wù)的session。去除掉該屬性就好了

          posted @ 2017-11-24 15:14 楊軍威 閱讀(185) | 評(píng)論 (0)編輯 收藏

          Java泛型

          2. 泛型類(lèi):泛型只在編譯時(shí)期有效,編譯后的字節(jié)碼文件中不存在有泛型信息!

          1. 泛型方法:

          public class GenericDemo {



          // 定義泛型方法
          public <K,T> T save(T t,K k) {
          return null;
          }
          // 測(cè)試方法
          @Test
          public void testMethod() throws Exception {
          // 使用泛型方法:  在使用泛型方法的時(shí)候,確定泛型類(lèi)型
          save(1.0f, 1);
          }
          }

          2. 泛型類(lèi):

          public class GenericDemo<T> {



          // 定義泛型方法
          public <K> T save(T t,K k) {
          return null;
          }
          public void update(T t) {

          }

             

              // 測(cè)試方法

              @Test

              public void testMethod() throws Exception {

                 

                  // 泛型類(lèi):  在創(chuàng)建愛(ài)泛型類(lèi)對(duì)象的時(shí)候,確定類(lèi)型

                  GenericDemo<String> demo = new GenericDemo<String>();

                  demo.save("test", 1);

              }

          }
          3. 泛型接口:
          /**
           * 泛型接口
           * @author Jie.Yuan
           *
           * @param <T>
           */
          public interface IBaseDao<T> {
          void save(T t );
          void update(T t );
          }
          泛型接口類(lèi)型確定: 實(shí)現(xiàn)泛型接口的類(lèi)也是抽象,那么類(lèi)型在具體的實(shí)現(xiàn)中確定或創(chuàng)建泛型類(lèi)的時(shí)候確定。
          泛型的反射

          /**

           * 所有dao的公用的方法,都在這里實(shí)現(xiàn)

           * @author Jie.Yuan

           *

           */

          public class BaseDao<T>{

             

              // 保存當(dāng)前運(yùn)行類(lèi)的參數(shù)化類(lèi)型中的實(shí)際的類(lèi)型

          private Class clazz;


          // 表名
          private String tableName;
          // 構(gòu)造函數(shù): 1. 獲取當(dāng)前運(yùn)行類(lèi)的參數(shù)化類(lèi)型; 2. 獲取參數(shù)化類(lèi)型中實(shí)際類(lèi)型的定義(class)
          public BaseDao(){
          //  this  表示當(dāng)前運(yùn)行類(lèi)  (AccountDao/AdminDao)
          //  this.getClass()  當(dāng)前運(yùn)行類(lèi)的字節(jié)碼(AccountDao.class/AdminDao.class)
          //  this.getClass().getGenericSuperclass();  當(dāng)前運(yùn)行類(lèi)的父類(lèi),即為BaseDao<Account>
          //                                           其實(shí)就是“參數(shù)化類(lèi)型”, ParameterizedType   
          Type type = this.getClass().getGenericSuperclass();
          // 強(qiáng)制轉(zhuǎn)換為“參數(shù)化類(lèi)型”  【BaseDao<Account>】
          ParameterizedType pt = (ParameterizedType) type;
          // 獲取參數(shù)化類(lèi)型中,實(shí)際類(lèi)型的定義  【new Type[]{Account.class}】
          Type types[] =  pt.getActualTypeArguments();
          // 獲取數(shù)據(jù)的第一個(gè)元素:Accout.class
          clazz = (Class) types[0];
          // 表名  (與類(lèi)名一樣,只要獲取類(lèi)名就可以)
          tableName = clazz.getSimpleName();
          }

          /**
          * 主鍵查詢(xún)
          * @param id 主鍵值
          * @return      返回封裝后的對(duì)象
          */
          public T findById(int id){
          /*
          * 1. 知道封裝的對(duì)象的類(lèi)型
          * 2. 表名【表名與對(duì)象名稱(chēng)一樣, 且主鍵都為id】
          * 即,
          *  ---》得到當(dāng)前運(yùn)行類(lèi)繼承的父類(lèi)  BaseDao<Account>
          *   ----》 得到Account.class
          */
          String sql = "select * from " + tableName + " where id=? ";
          try {
          return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<T>(clazz), id);
          } catch (SQLException e) {
          throw new RuntimeException(e);
          }
          }
          /**
          * 查詢(xún)?nèi)?/span>
          * @return
          */
          public List<T> getAll(){
          String sql = "select * from " + tableName ;
          try {
          return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<T>(clazz));
          } catch (SQLException e) {
          throw new RuntimeException(e);
          }
          }
          }

          posted @ 2017-11-14 17:34 楊軍威 閱讀(123) | 評(píng)論 (0)編輯 收藏

          log4j整合springmvc在web.xml中的配置

          <!-- Log4j配置 -->
          <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:config/log4j.properties</param-value>
            </context-param>
            <context-param>  
                  <param-name>log4jRefreshInterval</param-name>  
                  <param-value>60000</param-value>  
              </context-param>  
              <listener>  
                  <listener-class>  
                      org.springframework.web.util.Log4jConfigListener  
                  </listener-class>  
              </listener>  
          <!-- end -->  
            
              <listener>  
                  <listener-class>  
                      org.springframework.web.context.ContextLoaderListener  
                  </listener-class>  
              </listener> 
          配置的順序不能亂!

          posted @ 2017-11-14 17:17 楊軍威 閱讀(1559) | 評(píng)論 (0)編輯 收藏

          java自定義annotation模仿hibernate注解實(shí)體和表對(duì)象

          package com.annotation;
          import static java.lang.annotation.ElementType.FIELD;
          import static java.lang.annotation.ElementType.METHOD;
          import java.lang.annotation.Documented;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(value={ FIELD, METHOD})
          public @interface Column {
          String ColumnName();
          }
          package com.annotation;
          import static java.lang.annotation.ElementType.FIELD;
          import static java.lang.annotation.ElementType.METHOD;
          import java.lang.annotation.Documented;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(value={ FIELD, METHOD})
          public @interface Id {
          }
          package com.annotation;
          import static java.lang.annotation.ElementType.TYPE;
          import java.lang.annotation.Documented;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;
          @Documented
          @Retention(RetentionPolicy.RUNTIME)
          @Target(value={  TYPE})
          public @interface Table {
          String tableName();
          }
          package com;
          import java.lang.reflect.Field;
          import java.lang.reflect.ParameterizedType;
          import java.lang.reflect.Type;
          import com.annotation.Column;
          import com.annotation.Id;
          import com.annotation.Table;
          public class BaseDao<T,Pk> {
          private Class<T> persistentClass;
          @SuppressWarnings("unused")
          private Class<Pk> persistentPK;
          private String tableName;//表名稱(chēng)
          private String id;//主鍵
          public BaseDao() {
          ParameterizedType ptype=(ParameterizedType) this.getClass().getGenericSuperclass();
          Type[] types = ptype.getActualTypeArguments();
          for (Type type : types) {
          System.out.println(type.toString());
          }
          this.persistentPK = (Class<Pk>) types[1];
          this.persistentClass = (Class<T>) types[0];
          Table table = this.persistentClass.getAnnotation(Table.class);
          tableName=table.tableName();
          Field[] fields = this.persistentClass.getDeclaredFields();
          for (Field field : fields) {
          field.setAccessible(true);
          Id annotationId = field.getAnnotation(Id.class);
          if(annotationId != null){
          Column annotationCo = field.getAnnotation(Column.class);
          id=annotationCo.ColumnName();
          break;
          }
          }
          }
          public T getT(T t){
          System.out.println(tableName);
          System.out.println(id);
          return t;
          }
          }
          package com;
          import com.annotation.Column;
          import com.annotation.Id;
          import com.annotation.Table;
          @Table(tableName = "t_user")
          public class User {
          @Id
          @Column(ColumnName = "uid")
          private String id="1";
          public String getId()  {
          return id;
          }
          public void setId(String id) {
          this.id = id;
          }
          public User(String id) {
          super();
          this.id = id;
          System.out.println("有參數(shù)");
          }
          public User() {
          System.out.println("沒(méi)有參數(shù)");
          }
          }
          package com;
          public class UserDao extends BaseDao<User, String>{
          }

          posted @ 2017-11-14 17:15 楊軍威 閱讀(244) | 評(píng)論 (0)編輯 收藏

          僅列出標(biāo)題
          共43頁(yè): First 上一頁(yè) 4 5 6 7 8 9 10 11 12 下一頁(yè) Last 

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 富裕县| 都江堰市| 康定县| 香格里拉县| 平凉市| 彩票| 海兴县| 平塘县| 睢宁县| 浦北县| 弥勒县| 宁蒗| 车致| 安图县| 临城县| 门源| 秦皇岛市| 邮箱| 民乐县| 大足县| 治县。| 九江市| 吴桥县| 合川市| 方城县| 新巴尔虎左旗| 额尔古纳市| 崇明县| 温泉县| 建平县| 六盘水市| 铜陵市| 九龙坡区| 酉阳| 鄂托克前旗| 平泉县| 泗洪县| 沙坪坝区| 石棉县| 于田县| 阿瓦提县|