使用Java配置搭建SSH框架(2)

@Configuration
@PropertySource(
        value={"classpath:ds/ds-jdbc.properties"},
        ignoreResourceNotFound = true)//加载在resources下的ds-jdbc.properties
public class HibernateConfig {
    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String jdbcURL;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    //配置sessionFactory
    @Bean
    public SessionFactory sessionFactoryBean() {
        try {
            LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
            lsfb.setDataSource(dataSource());
            //扫描实体类
            lsfb.setPackagesToScan("scau.zzf.entity");
            Properties props = new Properties();
            //设置方言 ,采用的是MySql
            props.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
            lsfb.setHibernateProperties(props);
            lsfb.afterPropertiesSet();
            SessionFactory object = lsfb.getObject();
            return object;
        } catch (IOException e) {
            return null;
        }
    }
    //配置DataSource
    @Bean
    public DataSource dataSource(){
        BasicDataSource ds=new BasicDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(jdbcURL);
        ds.setUsername(username);
        ds.setPassword(password);

return ds;
    }
  @Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

实体类如下

@Entity
@Table(catalog = "hibernate",name = "users")
public class User {
    @Column
    private String username;
    @Column
    private String password;
    @Column
    private String sex;
    @Column
    private String address;
    @Column
    private int enabled;

//GeneratedValue覆盖@id的默认访问策略
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
//    @GenericGenerator是在hibernate中定义的,使用hibernate内置的各种主键生成粗略生成主键值
//    @GenericGenerator(name = "hibernate-uuid",strategy = "uuid")
//    @GeneratedValue(generator = "hibernate-uuid")
    private int id;

public int getEnabled() {
        return enabled;
    }

public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

public String getAddress() {
        return address;
    }

public void setAddress(String address) {
        this.address = address;
    }

public String getSex() {
        return sex;
    }

public void setSex(String sex) {
        this.sex = sex;
    }

public String getUsername() {
        return username;
    }

public void setUsername(String username) {
        this.username = username;
    }

public int getId() {
        return id;
    }

public void setId(int id) {
        this.id = id;
    }

public String getPassword() {
        return password;
    }

public void setPassword(String password) {
        this.password = password;
    }
}

在Test的环境下进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebConfig.class, RootConfig.class})
@WebAppConfiguration
public class ServiceTest {


    @Autowired
    private UserService userService;
    @Test
    public void userServiceTest(){

User user=userService.findUser(1);
        System.out.println(user.getUsername());
    }

}

在test的环境下,类上的三个注解是一定要配置的,不然会出错。

下面关于SSH相关的文章您也可能喜欢,不妨参考下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/76d03724caf72818ee5622d63367c363.html