Cieľom cvičenia je oboznámiť sa s atribútmi abstract a final XSD schémy.
Cieľom cvičenia je oboznámiť sa s projektom Spring Data a napojením Spring aplikácie na MySQL databázu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.3.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.22.Final</version> </dependency> |
1 2 3 4 5 6 7 8 9 |
public class Book { private int id; private String title; private String author; public String toString() { return id + ":" + title + ":" + author; } } |
1 2 3 4 5 6 |
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/asos?useUnicode=yes&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> |
1 2 3 4 5 6 7 8 9 10 |
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(SQL); while (sqlRowSet.next()) { int id = sqlRowSet.getInt(1); String title = sqlRowSet.getString(2); String author = sqlRowSet.getString(3); Book book = new Book(id, title, author); System.out.println(book); } |
1 2 3 4 5 6 7 8 9 |
public class BookMapper implements RowMapper<Book> { public Book mapRow(ResultSet rs, int rowNum) throws SQLException { int id = rs.getInt(1); String title = rs.getString(2); String author = rs.getString(3); return new Book(id, title, author); } } |
1 2 3 4 |
List<Book> books = jdbcTemplate.query(SQL, new BookMapper()); for (Book book : books) { System.out.println(book); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<jpa:repositories base-package="sk.stuba.fei.uim.asos.spring.data"/> <context:component-scan base-package="sk.stuba.fei.uim.asos.spring.data"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="sk.stuba.fei.uim.asos.spring.data"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="jpaProperties"> <props> <prop key="hibertnate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> |
1 2 3 4 5 6 7 |
@Repository public interface BookRepository extends JpaRepository<Book, Integer> { List<Book> findAllByAuthor(String author); Page<Book> findAllByAuthor(Pageable pageable, String author); } |
1 2 3 4 5 |
BookRepository bean = context.getBean(BookRepository.class); books = bean.findAll(); for (Book book : books) { System.out.println(book); } |
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.