java学习

java学习

 

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;
}
//引用传递,传递的是对象的引用,指向的还是原来的对象
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 杨军威 阅读(127) | 评论 (0)编辑 收藏

子线程执行10次,主线程执行100次,然后再子线程执行10次,主线程执行100次,循环50次

package com.yjw.thread;
public class Test2 {
static class Businis {
boolean f = false;// true sub执行,false main执行
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 杨军威 阅读(243) | 评论 (0)编辑 收藏

线程重写了run方法并传入一个新的线程时执行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();
}
线程重写了run方法,又传入了新的线程,此线程只执行重写了run方法的方法,因为根据面向对象的原则,子类重写了父类的方法,只执行子类的方法。

posted @ 2017-12-08 09:39 杨军威 阅读(230) | 评论 (0)编辑 收藏

浏览器触发ajax请求的次数限制

一个页面同时触发ajax请求的次数不能过多,同时触发的次数不要超过6个,不然过多的ajax会被浏览器阻塞,不能实现异步请求。

posted @ 2017-12-06 14:56 杨军威 阅读(349) | 评论 (0)编辑 收藏

负载均衡出现404

负载均衡时,如果客户端访问出现404的情况时,有可能是负载均衡映射到了空的服务造成的,需要检查负载均衡对应的后台主机是否存在。

posted @ 2017-12-06 14:54 杨军威 阅读(166) | 评论 (0)编辑 收藏

oracle建立普通用户

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 杨军威 阅读(152) | 评论 (0)编辑 收藏

createQuery is not valid without active transaction

在DAO文件中,通过sessionFactory.getCurrentSession()来获取会话,报异常:org.hibernate.HibernateException: createQuery is not valid without active transaction。经过实验,发现将Hibernate的配置文件中的<property name="current_session_context_class">thread</property>属性去掉就好了。原来"current_session_context_class"属性的意思是,设置当前会话的上下文环境,如果设置为thread,那么同一线程则共享同一session会话。因此通过getCurrentSession()得到的session,是同一线程上的session,而不是Spring管理的那个能够自动开启事务的session。去除掉该属性就好了

posted @ 2017-11-24 15:14 杨军威 阅读(177) | 评论 (0)编辑 收藏

Java泛型

2. 泛型类:泛型只在编译时期有效,编译后的字节码文件中不存在有泛型信息!

1. 泛型方法:

public class GenericDemo {



// 定义泛型方法
public <K,T> T save(T t,K k) {
return null;
}
// 测试方法
@Test
public void testMethod() throws Exception {
// 使用泛型方法:  在使用泛型方法的时候,确定泛型类型
save(1.0f, 1);
}
}

2. 泛型类:

public class GenericDemo<T> {



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

}

   

    // 测试方法

    @Test

    public void testMethod() throws Exception {

       

        // 泛型类:  在创建爱泛型类对象的时候,确定类型

        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 );
}
泛型接口类型确定: 实现泛型接口的类也是抽象,那么类型在具体的实现中确定或创建泛型类的时候确定。
泛型的反射

/**

 * 所有dao的公用的方法,都在这里实现

 * @author Jie.Yuan

 *

 */

public class BaseDao<T>{

   

    // 保存当前运行类的参数化类型中的实际的类型

private Class clazz;


// 表名
private String tableName;
// 构造函数: 1. 获取当前运行类的参数化类型; 2. 获取参数化类型中实际类型的定义(class)
public BaseDao(){
//  this  表示当前运行类  (AccountDao/AdminDao)
//  this.getClass()  当前运行类的字节码(AccountDao.class/AdminDao.class)
//  this.getClass().getGenericSuperclass();  当前运行类的父类,即为BaseDao<Account>
//                                           其实就是“参数化类型”, ParameterizedType   
Type type = this.getClass().getGenericSuperclass();
// 强制转换为“参数化类型”  【BaseDao<Account>】
ParameterizedType pt = (ParameterizedType) type;
// 获取参数化类型中,实际类型的定义  【new Type[]{Account.class}】
Type types[] =  pt.getActualTypeArguments();
// 获取数据的第一个元素:Accout.class
clazz = (Class) types[0];
// 表名  (与类名一样,只要获取类名就可以)
tableName = clazz.getSimpleName();
}

/**
* 主键查询
* @param id 主键值
* @return      返回封装后的对象
*/
public T findById(int id){
/*
* 1. 知道封装的对象的类型
* 2. 表名【表名与对象名称一样, 且主键都为id】
* 即,
*  ---》得到当前运行类继承的父类  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);
}
}
/**
* 查询全部
* @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 杨军威 阅读(111) | 评论 (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 杨军威 阅读(1548) | 评论 (0)编辑 收藏

java自定义annotation模仿hibernate注解实体和表对象

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;//表名称
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("有参数");
}
public User() {
System.out.println("没有参数");
}
}
package com;
public class UserDao extends BaseDao<User, String>{
}

posted @ 2017-11-14 17:15 杨军威 阅读(230) | 评论 (0)编辑 收藏

仅列出标题
共43页: First 上一页 4 5 6 7 8 9 10 11 12 下一页 Last 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜