Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Camiseta {
- private int id;
- private int numero;
- private Marca marca;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getNumero() {
- return numero;
- }
- public void setNumero(int numero) {
- this.numero = numero;
- }
- public Marca getMarca() {
- return marca;
- }
- public void setMarca(Marca marca) {
- this.marca = marca;
- }
- }
- public class Equipo {
- private int id;
- private String nombre;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- }
- public class Jugador {
- private int id;
- private String nombre;
- private Equipo equipo;
- private Camiseta camiseta;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public Equipo getEquipo() {
- return equipo;
- }
- public void setEquipo(Equipo equipo) {
- this.equipo = equipo;
- }
- public Camiseta getCamiseta() {
- return camiseta;
- }
- public void setCamiseta(Camiseta camiseta) {
- this.camiseta = camiseta;
- }
- }
- public class Marca {
- private int id;
- private String nombre;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- }
- import com.mitocode.beans.Marca;
- public interface DAOMarca {
- public void registrar(Marca marca) throws Exception;
- }
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import javax.sql.DataSource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import com.mitocode.beans.Marca;
- @Repository //estereotipo de una determinada capa
- public class DAOMarcaImpl implements DAOMarca {
- @Autowired
- private DataSource dataSource;
- public void registrar(Marca marca) throws Exception {
- // TODO Auto-generated method stub
- String sql = "INSERT INTO marca(marca_id, marca_nombre) VALUES (?,?)";
- Connection con = null;
- try {
- con = dataSource.getConnection();
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setInt(1, marca.getId());
- ps.setString(2, marca.getNombre());
- ps.executeUpdate();
- ps.close();
- } catch (Exception e) {
- // TODO: handle exception
- }
- finally {
- if (con != null) {
- con.close();
- }
- }
- }
- }
- import com.mitocode.beans.Marca;
- public interface ServiceMarca {
- public void registrar(Marca marca) throws Exception;
- }
- import org.springframework.stereotype.Service;
- import com.mitocode.beans.Marca;
- import com.mitocode.dao.DAOMarca;
- @Service
- public class ServiceMarcaImpl implements ServiceMarca {
- private DAOMarca daoMarca;
- public void registrar(Marca marca) throws Exception {
- // TODO Auto-generated method stub
- try {
- daoMarca.registrar(marca);
- } catch (Exception e) {
- // TODO: handle exception
- }
- finally {
- }
- }
- }
- Marca marca = new Marca();
- marca.setId(666);
- marca.setNombre("Mi marca");
- ApplicationContext appContext = new ClassPathXmlApplicationContext("com/mitocode/xml/beans.xml");
- ServiceMarca sm = (ServiceMarca) appContext.getBean("serviceMarcaImpl");
- try {
- sm.registrar(marca);
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println("Error insercion: "+e);
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
- <context:component-scan base-package="com.mitocode"></context:component-scan>
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/db_spring"></property>
- <property name="username" value="root"></property>
- <property name="password" value=""></property>
- </bean>
- </beans>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.mitocode</groupId>
- <artifactId>springbd</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>springbd</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>4.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.37</version>
- </dependency>
- </dependencies>
- </project>
Add Comment
Please, Sign In to add comment