智能销售系统_day01

1.项目搭建准备

1.建立maven-web项目

2.导入项目需要的jar包




  4.0.0

  com.xwq
  aisell
  1.0-SNAPSHOT
  war

  aisell Maven Webapp
  
  http://www.example.com
  
  
    UTF-8
    4.2.5.RELEASE
    4.3.8.Final
    1.9.0.RELEASE
    2.5.0
    1.6.1
  
  
    
    
      org.springframework
      spring-core
      ${org.springframework.version}
    
    
      org.springframework
      spring-context
      ${org.springframework.version}
    
    
    
      org.springframework
      spring-context-support
      ${org.springframework.version}
    

    
      org.springframework
      spring-tx
      ${org.springframework.version}
    
    
      org.springframework
      spring-jdbc
      ${org.springframework.version}
    
    
      org.springframework
      spring-orm
      ${org.springframework.version}
    
    
      org.springframework
      spring-aop
      ${org.springframework.version}
    
    
      org.springframework
      spring-test
      ${org.springframework.version}
      test
    
    
    
      org.springframework
      spring-web
      ${org.springframework.version}
    
    
      org.springframework
      spring-webmvc
      ${org.springframework.version}
    
    
    
      org.apache.commons
      commons-io
      1.3.2
    
    
    
      commons-fileupload
      commons-fileupload
      1.2.2
    
    
    
      com.fasterxml.jackson.core
      jackson-core
      ${com.fasterxml.jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      ${com.fasterxml.jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${com.fasterxml.jackson.version}
    
    
    
      org.hibernate
      hibernate-core
      ${org.hibernate.version}
    
    
      org.hibernate
      hibernate-entitymanager
      ${org.hibernate.version}
    
    
    
      org.springframework.data
      spring-data-jpa
      ${spring-data-jpa.version}
    
    
    
      com.github.wenhao
      jpa-spec
      3.1.1
      
      
        
          *
          *
        
      
    

    
      commons-dbcp
      commons-dbcp
      1.2.2
    

    
      mysql
      mysql-connector-java
      5.1.6
    

    
      org.apache.commons
      commons-lang3
      3.5
    
    
    
      junit
      junit
      4.12
      test
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      
      provided
    
    
    
      org.slf4j
      slf4j-api
      ${org.slf4j.version}
    
    
      org.slf4j
      slf4j-log4j12
      ${org.slf4j.version}
      runtime
    
    
      log4j
      log4j
      1.2.14
    
    
    
      org.apache.velocity
      velocity
      1.6
    
    
    
      org.apache.shiro
      shiro-all
      1.4.0
      pom
    
    
    
      org.apache.shiro
      shiro-spring
      1.4.0
    
    
    
      org.apache.poi
      poi
      3.11
    
    
      org.apache.poi
      poi-ooxml
      3.11
    
    
    
    
      net.coobird
      thumbnailator
      0.4.6
    
    
    
      quartz
      quartz
      1.5.2
    
    
    
      javax.mail
      mail
      1.4.1
    
  
  
    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.8
          1.8
        
      
      
        org.mortbay.jetty
        jetty-maven-plugin
        8.1.15.v20140411
        
          9966
          foo
          
            /
          
        
      
    
  

3.配置spring的配置文件




    
    
    
    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
    
        
        
        
        
        
        
        
            
                
                
                
                
                
                
            
        
    
    
    
    
        
    
    
    

    
    
    

4.配置数据库db.properties

5.创建domain,抽取父类

由于domain中都有id属性,且为了方便以后扩展功能,抽取父类,并使用@MappedSuperclass标明这是一个父类

@MappedSuperclass
public class BaseDomain {
    @Id
    @GeneratedValue
    protected Long id;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

2.SpringDataJpa使用

1.pom.xml配置
  • 1.基本的SSJ的导入 - 2.SpringDataJpa

      org.springframework.data
      spring-data-jpa
      ${spring-data-jpa.version}
 
2.SpringDataJpa基本配置
(1).配置对应的Spring-data-jpa

(2).继承JpaRepository(指明domain类型和主键类型)
interface EmployeeRepository extends JpaRepository, JpaSpecificationExecutor
(3).完成相应的CRUD
employeeRepository.findAll();
employeeRepository.findOne(Long id);
employeeRepository.save(对象); //添加或者修改
employeeRepository.delete(id/对象);
employeeRepository.findAll(Pageable) -> 分页
	Pageable pageable = new PageRequest(0, 5); //0就是第一页
employeeRepository.findAll(Sort) -> 排序
	Sort sort = new Sort(Sort.Direction.DESC,"username");

如果要把分页和排序结合起来: (把排序对象传到分页中)

new PageRequest(0, 5,sort);
(4).高级查询
①.名称规则(按规范定义方法,SpringDataJpa会自动代理实现接口)
findByUsername(String username) //根据名称查询 username = ?
findByUsernameLike(String username) //username like ?
findByUsernameLikeAndEmailLike(String username,String email)  // username like ? and email like ?
②.Query注解
//jpql
@Query("select o from Employee o where o.name like ?1")
@Query("select o from Employee o where o.name like :name")
//原生sql
@Query(nativeQuery = true,value="select * from employee")
(5).高级查询与分页
①.JpaSpecificationExecutor

可以不写SQL也能够完成功能
1.我们的Repository继承JpaSpecificationExecutor

EmployeeRepository extends JpaRepository,JpaSpecificationExecutor

2.findAll的查询方法

employeeRepository.findAll(Specification spec)
    employeeRepository.findAll(Specification spec,Pageable pageable)
    new Specification{
        //root:(根,表)获取字段  
        //query:where,group by ,order by...
        //cb: 条件判断(一个或者多个)
         @Override
        public Predicate toPredicate(Root root, 
                                    CriteriaQuery query, 
                                    CriteriaBuilder cb) {
            Path path = root.get("username");//拿到要做查询的字段
            Predicate p = cb.like(path, "%1%");//like代表做模糊查询,后面就是它的条件值
            return p;
        }
    }
2.jpa-spec

    com.github.wenhao
    jpa-spec
    3.1.0

//第一个参数: true -> 条件过滤启用
//第二个参数: 需要过滤的属性
//第三个参数: 过滤条件的 ?对应的值
Specification specification = Specifications.and()
            .eq(StringUtils.isNotBlank(request.getName()), "name", name)
            .gt(Objects.nonNull(request.getAge()), "age", 18)
            .build();

你可能感兴趣的:(java项目,SSJ,智能销售系统,maven)