Spring boot +cassandra+maven学习笔记

pom.xml

    
            org.springframework.boot
            spring-boot-starter-data-cassandra
        
        
            com.datastax.cassandra
            cassandra-driver-core
            3.5.0
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
             

application.property

spring.data.cassandra.cluster-name=Test Cluster
spring.data.cassandra.keyspace-name= myspace
spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.port=9042

借用之前cassandra学习建了user表,

user.java

@Table
public class User {

    @PrimaryKey
    private UUID id;

    private String userName;

    @Override
    public String toString() {
        return String.format("User[id=%s, userName='%s']", this.id,
                this.userName);
    }
}

userDao.java

@Repository
public class UserDao{
	@Autowired
	private CassandraTemplate cassandraTemplate; 
	
	public List getAll() {
		return cassandraTemplate.select("SELECT ID, user_name FROM user", User.class);
		
	}  
}

UserController.java

@RestController
public class UserController {
	@Autowired
	private UserDao userDao;  
	@RequestMapping("/list")
	public List list() {
		return userDao.getAll();
	}
}

你可能感兴趣的:(Cassandra,spring,boot)